When I build a Go binary, I usually do something like this:
go build -ldflags "-X main.basedir somevar" -o mybuilddir/bin/myfile mypackage/main"
this builds the binary and places it in a custom directory. But this doesn't keep the "intermediate" package files beneath pkg/
, which would speed up the next compilation runs.
The solution would be go install
, but I cannot specify an output directory. It seems to be possible to set the binary directory with GOBIN
, but I am unable to specify the name of the executable (always main
).
What is a possible solution to this problem?
main
) .a
) This is the src
directory of GOPATH
:
GOPATH/src$ tree
.
└── mypackage
├── packagea
│ └── packagea.go
├── packageb
│ └── packageb.go
└── main
└── mypackage.go
With go install
, the package files ( .a
) are created in $GOPATH/pkg
, with go build
, I can't find the .a
files anywhere.
Update Nov. 2017: Go 1.10 (Q1 2018) will add caching for go build and go install: see " go build
rebuilds unnecessarily ".
Original answer (2014)
With
go install
, the package files (.a
) are created in$GOPATH/pkg
, withgo build
, I can't find the.a
files anywhere.
As mentioned in " How does the go build command work ? "
The final step is to pack the object file into an archive file,
.a
, which the linker and the compiler consume.Because we invoked
go build
on a package, the result is discarded as$WORK
is deleted after the build completes .
If we invokego install -x
two additional lines appear in the output
mkdir -p /home/dfc/go/pkg/linux_arm/crypto/
cp $WORK/crypto/hmac.a /home/dfc/go/pkg/linux_arm/crypto/hmac.a
This demonstrates the difference between
go build
andinstall
;
build
builds,install
builds then installs the result to be used by other builds.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.