VIM Go Syntastic: main redeclared
Asked Answered
H

6

17

VIM Syntastic plugin works well with .go file. But sometimes I want to have several go files in the same folder, and each with the main() method, so that I can go run xxx each file(for presentation). This will cause the error when I save the second file(prog2.go):

main redeclared in the block previous declaration at prog1.go

How can I tell Syntastic to ignore these errors?

Update 1

The official go talks like Rob Pike's "Go Concurrency Patterns" and Francesc Campoy Flores' "Twelve Go Best Practices" all put the source file in the same folder. So this question is not about the best practice to run go file, but how to suppress or ignore this warning.

Update 2

After I file an issue here, the author answered my question clearly. That's just what I needed. Thanks for all.

Hijack answered 3/3, 2014 at 23:53 Comment(0)
Q
34

You cannot, because it's a genuine error.

If you have multiple .go files in a single package (i.e. package main) then you can only have one main() function.

You should be using go build to build your application into an executable binary. go run is a quick 'hack' for testing single files, not for complete programs.

Your options:

  1. Put them in the same package and use go build.
  2. Split your 'programs' into separate packages. e.g. $GOPATH/src/mypkg1 and $GOPATH/src/mypkg2
  3. Use a scripting language.

I'd also suggest reading How To Write Go Code for how to manage packages.

Quid answered 4/3, 2014 at 3:46 Comment(2)
The option 1 is what I prefer and that's the official Go talks all put the .go file in the same folder. See the official talk bestpractices here and other talks in the repository. But the VIM Syntastic will warn every time when saving these files in the same folder. That's what I asked for. I think there should be a feature to suppress the warning, right? We know it's not an error in this condition.Hijack
@Hijack Those are example programs. As you'll notice, they are all package main and yet each have main() blocks, because they are meant to be run in isolation. That repo is a little misleading (unfortunately). Just split them into separate folders. If you really must change things, raise an issue on the Syntastic GitHub repo and ask the maintainer(s).Quid
T
3

The reason is, There can be only one main function in a package.

Trammel answered 7/5, 2020 at 1:3 Comment(0)
J
2

If you are using vs code.

kindly turn off. extension go. issue solved.

Jeanene answered 31/5, 2021 at 9:4 Comment(0)
A
1

even though it shows error you can run go run command to test/run that file

$ go run file.go
Apery answered 12/10, 2018 at 14:4 Comment(0)
Z
1

I'm learning go and it's handy to put many small "func main" files in the same directory and try each of them, would love to silence the 'main redeclared in this block'

Zeeland answered 22/10, 2020 at 22:30 Comment(0)
S
0

if you use vim-go plugin, you can add the following to your .vimrc.

let mapleader = ","    
autocmd FileType go nmap <Leader>rs  :<C-u>GoRun %<cr>

Just hitting ,rs, you can get result.

Stopped answered 18/10, 2018 at 1:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.