VIM Go Syntastic: main redeclared

user2647545 picture user2647545 · Mar 4, 2014 · Viewed 14.8k times · Source

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.

Answer

elithrar picture elithrar · Mar 4, 2014

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.