How to package and install Go CLI tools and libraries?

blov · 2016-09-02 21:00:02 · 537 次点击    
这是一个分享于 2016-09-02 21:00:02 的资源,其中的信息可能已经有所发展或是发生改变。

I have an early prototype of a go-chop (and chomp) library and set of binaries, at https://github.com/mcandre/go-chop

But I'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.

Is there a way to organize my Go project to facilitate this? What's the command(s) that other Go users would use to install my stuff?


评论:

shovelpost:

Your Makefile is 40% of the repository's code...

Anyways, if you want to have multiple commands, the most common way I've seen is to have a cmd folder then two or more folders, one for each command which then will contain the main.go of the command.

By following that layout, when someone go gets your repo, they will also get all the commands.

So in your case, instead of cli-chomp.go and cli-chop.go you would have a cmd folder, containing chomp and chop folders containing cli-chomp.go and cli-chop.go respectively or you could even rename them as main.go.

Helpful article: Five suggestions for setting up a Go project

mcandre:

Your Makefile is 40% of the repository's code...

Lol yeah, that's been my crutch while I learn Go idioms.

When a user runs go get ..., 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?

shovelpost:

As far as I know, just doing go get github.com/someuser/somerepo 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 go get and then they can instantly use the commands of somerepo.

Helpful read: https://golang.org/doc/articles/go_command.html#tmp_3

howeyc:

I would recommend what shovelpost suggested.

I have something that I believe follows such a convention.

https://github.com/howeyc/ledger

The root is the package and the cmd directory has three separate executables.


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

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