Can you set GOOS and GOARCH for go generate?

blov · · 573 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m curious if I can set <code>GOOS</code> and <code>GOARC</code> to something besides my native system in <code>go generate</code> commands?</p> <p>I know these are set in <code>go generate</code> but I want to create a build process that will target an OS other than my development one.</p> <hr/>**评论:**<br/><br/>ovulateworld: <pre><p>What are you trying to do?</p> <p>It seems like you should be using <a href="https://golang.org/pkg/go/build/#hdr-Build_Constraints" rel="nofollow">build constraints</a> instead.</p></pre>Destructicorn: <pre><p>I had no idea this was a thing. I&#39;m trying to build for linux and developing on OSx. It looks like build constraints is exactly what I need. Still newish to the language, thanks for the help!</p> <p>Edit: After doing some addition research it seems like build constraints aren&#39;t quite what I want. They restrict what files can be built on any os. I want to build targeted at linux <code>GOOS=linux GOARCH=amd64 go build</code> created from my OSx machine. I would like to run <code>go generate</code> to do the build then deploy the application. </p> <p>This is what I want to do: <code> //go:generate GOOS=linux GOARCH=amd64 go build </code></p></pre>Akkifokkusu: <pre><p>This seems like a misuse of the <code>go:generate</code> tag, but you could probably get what you want by wrapping <code>go build</code> in a script that takes arguments.</p></pre>Destructicorn: <pre><p>Yeah, after doing some more research I decided to go with a Makefile instead. I think I was trying to force <code>go:generate</code> to do something it wasn&#39;t really meant to do.</p></pre>jkrippy: <pre><p>I had problems doing that and then running the generated binaries across different linux distributions. Maybe whatever was causing my problems has been fixed now but I switched to using docker for mac (which runs on OSX in a linux vm) and building using a go container instead and no problems since then. Definitely cool when your approach works though, you can even target Windows EXEs from your Mac this way!</p></pre>stuartcarnie: <pre><p>What are you trying to achieve. We could probable offer better advice if we knew what you were trying to do by adding those environment vars to <code>go generate</code></p></pre>

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

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