VIM Go Syntastic: main redeclared(VIM Go Syntastic:main 重复声明)

huangapple go评论106阅读模式
英文:

VIM Go Syntastic: main redeclared

问题

VIM的Syntastic插件在处理.go文件时效果很好。但有时我想在同一个文件夹中有几个go文件,并且每个文件都有main()方法,这样我就可以对每个文件使用go run xxx(用于演示)。当我保存第二个文件(prog2.go)时,就会出现以下错误:

main在块中重新声明,之前的声明在prog1.go中

我该如何告诉Syntastic忽略这些错误?

更新1

官方的Go讲座,如Rob Pike的《Go并发模式》和Francesc Campoy Flores的《十二个Go最佳实践》,都将源文件放在同一个文件夹中。所以这个问题不是关于运行go文件的最佳实践,而是如何抑制或忽略这个警告。

更新2

在我在这里提交了一个问题(https://github.com/scrooloose/syntastic/issues/1019#issuecomment-36782267)之后,作者清楚地回答了我的问题。这正是我需要的。谢谢大家。

英文:

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.

答案1

得分: 34

你不能这样做,因为这是一个真正的错误。

如果在一个包中有多个.go文件(即package main),那么你只能有一个main()函数。

你应该使用go build将你的应用程序构建成可执行二进制文件。go run只是用于测试单个文件的快速“hack”,而不是完整的程序。

你有以下几个选择:

  1. 将它们放在同一个包中,并使用go build
  2. 将你的“程序”拆分成单独的包。例如$GOPATH/src/mypkg1$GOPATH/src/mypkg2
  3. 使用脚本语言。

我还建议阅读《如何编写Go代码》(http://golang.org/doc/code.html)以了解如何管理包。

英文:

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.

答案2

得分: 3

原因是,在一个包中只能有一个主函数。

英文:

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

答案3

得分: 2

如果你正在使用VS Code,请关闭Go扩展。这样问题就解决了。

英文:

If you are using vs code.
> kindly turn off.
extension go.
issue solved.

答案4

得分: 1

即使它显示错误,你可以运行"go run"命令来测试/运行该文件。

$ go run file.go
英文:

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

$ go run file.go

答案5

得分: 1

我正在学习Go语言,将许多小的"func main"文件放在同一个目录下并逐个尝试是很方便的,但我希望能够消除"main在此块中重复声明"的提示信息。

英文:

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'

答案6

得分: 0

如果你使用vim-go插件,你可以将以下内容添加到你的.vimrc文件中:

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

只需按下",rs",你就可以得到结果。

英文:

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

let mapleader = &quot;,&quot;    
autocmd FileType go nmap &lt;Leader&gt;rs  :&lt;C-u&gt;GoRun %&lt;cr&gt;

Just hitting ,rs, you can get result.

huangapple
  • 本文由 发表于 2014年3月4日 07:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/22160094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定