dongpiansui8755 2016-06-07 13:13
浏览 31
已采纳

如何编写包含多个二进制文件的软件包?

I've seen this discussed in various places, with answers like 'use a cmd/foo, cmd/bar' type folder structure.

This does not work for me.

This works:

$ du -a
8   ./src/cmd/bin1/main.go
8   ./src/cmd/bin1
8   ./src/cmd/bin2/main.go
8   ./src/cmd/bin2
16  ./src/cmd
8   ./src/shared/foo/foo.go
8   ./src/shared/foo
8   ./src/shared
24  ./src

and building it:

go build ./src/cmd/bin2
go build ./src/cmd/bin1

However, I can't figure out what variation on:

go build ./src/...

I might need to build all such binaries in one step.

This layout:

$ du -a
8   ./cmd/bin1/main.go
8   ./cmd/bin1
8   ./cmd/bin2/main.go
8   ./cmd/bin2
16  ./cmd
8   ./src/shared/foo/foo.go
8   ./src/shared/foo
8   ./src/shared
8   ./src
24  .

Seems totally unusable. No combination of go build ... commands seems to build bin1 or bin2.

The best I can get is go build cmd/bin1/main.go which gives me a binary called 'main'. Not helpful.

So, specifically and in detail, including the go build command, that actually builds the individual binaries, how do you do this?

...and why is the default advice that people keep giving to use a top level cmd folder? How do you build these binaries if you do?

  • 写回答

3条回答 默认 最新

  • duanjuelu8874 2016-06-07 13:46
    关注

    If you don't want to install the binaries into $GOPATH/bin, you could do what other open source projects do, which is create a script.

    Most of the projects out there have make files and build scripts for producing multiple binaries.

    In your case, you could build a script that iterates over the packages in cmd, and run go build on each.

    cd $GOPATH/someProject
    for CMD in `ls cmd`; do
      go build ./cmd/$CMD
    done
    

    This results in:

    [root@node1 test]# ls $GOPATH/someProject
    bin1  bin2  cmd
    

    Couple of trending projects that you can look at:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测