Which flags should I do include when go building?

agolangf · · 487 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<hr/>**评论:**<br/><br/>gopher1717: <pre><p>go build ./cmd/app -o app[.exe] -v -i -ldflags=&#34;-s -w -X main.version=V1.0.0&#34;</p> <ul> <li><code>-o</code>: set output file</li> <li><code>-v</code>: verbose mode</li> <li><code>-i</code>: installs while building. Important for compiling speed, since this makes cache of packages at $GOPATH/pkg</li> <li><code>-ldflags</code>: <ul> <li><code>-s</code> and <code>-w</code>: makes executable smaller by stripping debug information</li> <li><code>-X variable=value</code>: useful to say variables, like version. You can see the last Git tag or commit hash, as example.</li> </ul></li> </ul></pre>Jelterminator: <pre><p>Best is to use git describe (--tags) for the version. It will use the last tag possibly appended with number of commits and hash of the current commit (but only if the last tag is not for the current commit) </p></pre>Sythe2o0: <pre><p>What do you want your program to do? You don&#39;t need to use any flags. </p></pre>hell_0n_wheel: <pre><p>Flags are good for integration testing (connecting to a real vs. a mock db), as one example.</p></pre>tmornini: <pre><p>I recommend you pull back and consider the incredible usefulness of external configuration via environment variables.</p> <p>Good stuff!</p></pre>hell_0n_wheel: <pre><p>Already doing that.</p> <p>I don&#39;t want to compile integration test code into my production executables.</p></pre>tmornini: <pre><p>Then only include it in your integration test code.</p></pre>hell_0n_wheel: <pre><p>I do, and I use flags to build that code.</p> <p>How else would you suggest to do this?</p></pre>tmornini: <pre><p>The code that is built to run your tests is not the code that is built to run your application.</p></pre>hell_0n_wheel: <pre><p>Uhh, we already covered that point, you seem to be telling me I&#39;m doing it wrong, you gonna share the right way or what?</p></pre>tmornini: <pre><p>It&#39;s possible we&#39;re talking about different issues.</p> <p>I thought you suggested using flags to build tests differently than production code, including things in tests that are not in production.</p> <p>My point is that you don&#39;t need to use flags to do that. I haven&#39;t said it before, but changing code for testing implies that you&#39;re not testing the production code, but some different version, negating the benefits.</p> <p>If you need code that is only in the tests, then only include that code in your tests. That way you don&#39;t need flags to not-include the code in your production code.</p></pre>LC2712: <pre><p>I see the one for version is very used</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

487 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传