dphnn333971 2018-06-01 16:07
浏览 81

使用ldflags设置Go变量与供应商文件夹冲突

I'm currently developing a small Go app and I want to set a specific variable (like Version, GitCommit, BuildID...etc.) at build or runtime (with go build or go run) by using the -ldflags option.

Because in my company we have several projects with the same base, I decided to extract the code with these variables in a separate "info" module which could be imported in every project.

Here's my problem, say I'm running the app like this:

go run -ldflags "-X git.mycompany.com/utils/info.Version=1.0.0" app.go

This works well, and the variable is set correctly even though the variable is not part of the "main" app but in a dependency.

Then I decided to deploy the app so I used the new dep tool to generate the vendor folder. Therefore, the "info" dependency is now in: vendor/git.mycompany.com/utils/info

Now when I run the same command as above, the said variable (Version) is not set anymore.

Am I missing something here ? As soon as I delete the vendor folder, everything works fine again. It's like this vendor folder is conflicting with the -ldflags option or something.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douxitao8170 2018-10-23 13:16
    关注

    We had exactly the same problem, after lots of research we stumbled upon the solution in a comment to GitHub issue: cmd/link: -X doesn't work for vendored packages.

    Solution: the full path name, relative to $GOPATH should be specified.

    It works when developing git.mycompany.com/utils/info because the full path is correct.
    It doesn't work for vendored dependencies because the full path from $GOPATH would be like git.mycompany.com/name-of/package/vendor/git.mycompany.com/utils/info.Version=1.0.0

    Unfortunately, no documentation seem to be present about this ( for further info look at the issue ) but as Dave Cheney points out in a comment:

    this is a side effect of the language way vendoring is implemented

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog