drxd54816 2019-08-06 23:30
浏览 547
已采纳

有没有办法一次构建多个基于Go模块的项目?

Previous to using Go modules I could compile everything in my GOPATH with go build ./....

If I try this in the root of a directory with multiple module based Go projects, it doesn't download the dependencies specified in go.mod and fails to find the packages the code depends on because it is looking for them in GOPATH.

I have 126 modules based projects and I'd rather not have to write a script to handle building them.

  • 写回答

1条回答 默认 最新

  • dongweiben5229 2019-08-07 14:43
    关注

    No, there isn't a way to do this using only the go command. Scripting this or using replace directives is the way to go. A simple script might be:

    find "$(go env GOPATH)/src" \
      -name vendor -prune -o \
      -name go.mod -exec bash -c 'cd "$(dirname {})" && go build ./...' \;
    

    Each go.mod file defines a workspace, and go build, go test, go list, go get commands only apply to packages within that workspace.

    Within a module, it's possible to build packages provided by other modules. For example, if you have a module with the path example.com/foo, and you require the module example.com/bar, you can go build example.com/bar/some/pkg. However, this will not use your local copy of example.com/bar; it will download that module into your module cache ($GOPATH/pkg/mod) and will build from there.

    You can use a local copy of another module using a replace directive. This is good for short term forks and co-development of related modules (usually in the same repository). However, replace directives only apply in the module where they are written. If another module requires your module, your replace directives are ignored.

    The goal of all of this is to make builds reproducible. Developers that use your modules will only be able to download your dependencies at specific versions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line