Go build multiple/nested packages?

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

Go build multiple/nested packages?

问题

我刚刚开始学习Go(所以没有经验),想知道Go是否支持任何形式的“构建所有源文件”,就像mvn install那样。

我的项目结构是:

src
  `-github.com
          `-myproject
               |- package1
               |     `- main.go
               `- package2
                     |- lib1_used_by_main.go
                     `- lib2_used_by_main.go

当我执行以下命令时:

cd src/github.com/myproject
go build

会出现src/github.com/myproject中没有可构建的Go源文件的错误,这是正确的,因为所有的源文件都在子包中。

是否有一条命令可以构建所有的子包,而不需要显式地列出每个子包?

英文:

I just started writing Go today (so 0 experience), and wonder if Go supports any form of "building all source files" like what mvn install does.

My project structure is

src
  `-github.com
          `-myproject
               |- package1
               |     `- main.go
               `- package2
                     |- lib1_used_by_main.go
                     `- lib2_used_by_main.go

When I do

cd src/github.com/myproject
go build

this fails with no buildable Go source files in src/github.com/myproject, which is kind of right, because all source files are in subpackages.

Is there a command to build all subpackages, without listing each of them explicitly?

答案1

得分: 54

在你使用cd命令切换到基础目录之后,使用go build ./...命令。注意,这里有三个点,表示省略号。这将递归地构建所有子目录。当然,你也可以在任何地方使用go build path/to/my/base/...命令,而不需要切换到该目录。

对于那些依赖于go/pkg目录的集成开发环境(IDE),比如SublimeText3与GoSublime,这非常有用。如果对一个依赖包进行更改,直到你构建该包并将其放置在go/pkg目录中,自动完成功能才会更新。

我的项目被分成多个包结构,所以我经常需要运行go build ./...来更新自动完成功能。

英文:

After you cd to the base directory, use go build ./... Note that there are 3 periods as it is an ellipsis. This will recursively build all subdirectories. Of course you can always do go build path/to/my/base/... from wherever without needing to cd to the directory.

This is very useful for those who use an IDE that relies on the go/pkg directory, such as SublimeText3 with <a href="https://packagecontrol.io/packages/GoSublime">GoSublime</a>. Making changes to a dependency package won't update the autocompletes until you build the package, which places it in the go/pkg directory.

My own projects are broken into a multiple package structure, so I frequently have to go build ./... to update my autocompletion.

huangapple
  • 本文由 发表于 2017年2月23日 02:14:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/42399242.html
匿名

发表评论

匿名网友

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

确定