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 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。