英文:
How to build binary in package in Golang?
问题
我在github.com/user中有一个名为project的项目:
src/
github.com/user/project
sub1/
main.go
sub1.exe (??)
sub2/
main.go
sub2.exe (??)
我正在尝试编译项目中的包。当我执行以下操作时:
$ cd github.com/user/project/sub1
$ go build
没有任何反应。go build似乎没有报错,但没有可执行文件生成。我该如何将包编译成可执行文件?
"go version go1.3 windows/amd64"
英文:
I have a project in github.com/user called project:
src/
github.com/user/project
sub1/
main.go
sub1.exe (??)
sub2/
main.go
sub2.exe (??)
I am trying to compile the package in my project. When I:
$ cd github.com/user/project/sub1
$ go build
Nothing happens. go build seems to finish without complaining, but there is no executable file. How can I build packages into executable?
"go version go1.3 windows/amd64"
答案1
得分: 10
无论你将文件命名为main.go
还是shubudoo.go
都没有关系。唯一重要的是,如果你想构建一个可执行文件(一个命令),你的文件必须以package main
开头。还有一点:Go语言完全没有"子包"的概念:对于编译器来说,所有的包都是平等的。文件系统的嵌套只是为了方便你使用。
英文:
It doesn't matter if you name your file main.go
or shubudoo.go
. The only thing what matters if you want to build an executable (a command) is that your files start with package main
. One more thing: Go has absolutely no notion of "subpackage": All packages are equal for the compiler. The filesystem nesting is for your convenience only.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论