<p>So I've been plugging away at code that I intend to transpile to javascript for a user interface piece with gopherjs. The biggest issue I was running into is part of my APIs were not going to be compatible in gopherjs. As a result, I assumed I was going to have to make a massive make file to handle what to compile and when. </p>
<p>Well, it turns out GoLang has my back! I was stoked to find out about build constraints and they don't seem to be talked about much. It seems that if we ever wanted desktop UI's to exists, these things would be our saving grace. </p>
<p><a href="https://golang.org/pkg/go/build/#hdr-Build_Constraints">https://golang.org/pkg/go/build/#hdr-Build_Constraints</a></p>
<hr/>**评论:**<br/><br/>danredux: <pre><p>I love build constraints.</p>
<p>I have a very weird use case. 98% of my code is in a public repo, but there is a private repo containing the final 2%. The public repo has stubs which the private repo overwrites.</p>
<p>Simple solution? Import the private repo as a package. However, that means the public package is not build-able, because it's missing an import.</p>
<p>Build constraints saved me, by hiding the import in a build-constrained file, I have to pass in a specific tag to have the import be included. Others can "go get" with no weird trickery but I can pass in a build tag and have my private overrides applied.</p></pre>titpetric: <pre><p>Ugh, the documentation here for this sucks. I had to dig through at least two github issues about it before figuring out how you do this. So, I suspect:</p>
<pre><code>// +build !private
something here...
</code></pre>
<p>and then run <code>go build -tags private</code> that would "include" your 2% package. How do you modify public repo stubs? Globals? init()? A more concrete example would be interesting.</p></pre>igknighted: <pre><p>Important to note, <code>-tags private</code> must be before any file names. Go is kinda specific when doing this.</p></pre>Xanarion: <pre><p>Do you have an example code how that is done?</p></pre>igknighted: <pre><p>Probably separate files that import that 2nd private repo. In addition to the <code>go build -tags private</code> flag that <a href="/u/titpetric" rel="nofollow">/u/titpetric</a> mentioned. </p>
<p>It can work like this: <a href="https://gist.github.com/protosam/a8900dde24c6fe3ecc1056749131471a" rel="nofollow">https://gist.github.com/protosam/a8900dde24c6fe3ecc1056749131471a</a></p></pre>Xanarion: <pre><p>Thanks, I get the idea now :) </p></pre>
Discovered build constraints today! Life just became easymode for cross-platform specific stuff.
xuanbao · · 396 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传