stackoverflower
stackoverflower

Reputation: 4073

Go build multiple/nested packages?

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?

Upvotes: 37

Views: 36233

Answers (1)

RayfenWindspear
RayfenWindspear

Reputation: 6284

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 GoSublime. 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.

Upvotes: 58

Related Questions