How to package and install Go CLI tools and libraries?

blov · · 395 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I have an early prototype of a go-chop (and chomp) library and set of binaries, at <a href="https://github.com/mcandre/go-chop" rel="nofollow">https://github.com/mcandre/go-chop</a></p> <p>But I&#39;m not sure of the best way to structure my Go stuff. Am I doing it right? Ideally, when other Go users go-get my project, they receive a working library that they can use, as well as a chop and chomp binary in PATH.</p> <p>Is there a way to organize my Go project to facilitate this? What&#39;s the command(s) that other Go users would use to install my stuff?</p> <hr/>**评论:**<br/><br/>shovelpost: <pre><p>Your Makefile is 40% of the repository&#39;s code...</p> <p>Anyways, if you want to have multiple commands, the most common way I&#39;ve seen is to have a <code>cmd</code> folder then two or more folders, one for each command which then will contain the <code>main.go</code> of the command. </p> <p>By following that layout, when someone go gets your repo, they will also get all the commands.</p> <p>So in your case, instead of <code>cli-chomp.go</code> and <code>cli-chop.go</code> you would have a <code>cmd</code> folder, containing <code>chomp</code> and <code>chop</code> folders containing <code>cli-chomp.go</code> and <code>cli-chop.go</code> respectively or you could even rename them as <code>main.go</code>.</p> <p>Helpful article: <a href="http://dave.cheney.net/2014/12/01/five-suggestions-for-setting-up-a-go-project" rel="nofollow">Five suggestions for setting up a Go project</a></p></pre>mcandre: <pre><blockquote> <p>Your Makefile is 40% of the repository&#39;s code...</p> </blockquote> <p>Lol yeah, that&#39;s been my crutch while I learn Go idioms.</p> <p>When a user runs <code>go get ...</code>, does Go automatically build the .go files, and even place the binaries on PATH? Or what would be the typical steps for a Go user to begin using the cmd/... programs from a Go package URL?</p></pre>shovelpost: <pre><p>As far as I know, just doing <code>go get github.com/someuser/somerepo</code> will install somerepo under your $GOPATH. Any packages will be installed under $GOPATH/pkg and any commands under $GOPATH/bin. So basically the only thing the user has to do is <code>go get</code> and then they can instantly use the commands of somerepo. </p> <p>Helpful read: <a href="https://golang.org/doc/articles/go_command.html#tmp_3" rel="nofollow">https://golang.org/doc/articles/go_command.html#tmp_3</a></p></pre>howeyc: <pre><p>I would recommend what shovelpost suggested.</p> <p>I have something that I believe follows such a convention.</p> <p><a href="https://github.com/howeyc/ledger" rel="nofollow">https://github.com/howeyc/ledger</a></p> <p>The root is the package and the cmd directory has three separate executables.</p></pre>

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

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