dreamMyDream2014 2014-11-18 08:39
浏览 71
已采纳

安装Go二进制文件,保留依赖项/软件包

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?

  • Custom destionation directory
  • Custom name (not main)
  • Keep intermediate generated package files (.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.

  • 写回答

1条回答 默认 最新

  • dqkx69935 2014-11-18 09:09
    关注

    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, with go 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 invoke go 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 and install;

    • build builds,
    • install builds then installs the result to be used by other builds.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效