如何在Golang中构建二进制包?

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

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.

huangapple
  • 本文由 发表于 2015年2月8日 23:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/28395808.html
匿名

发表评论

匿名网友

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

确定