Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure binary name in Go without changing path

Tags:

go

I'm developing a small tool with Go. And recently, I noticed that the tool needs to be invoked from a shell script, because it's using shell function.

Assume my tool is called atool. So, go build generates a binary atool, and my tool has a Go structure as github.com/myaccount/atool. Now, i want to build atool-cli binary with go build, and invoke it from shell script atool. How can I achieve this?

The only way coming in my mind is change go structure as github.com/myaccuont/atool-cli. But I don't want to do this because the already announced, and also, the path seems a bit funny name.

like image 837
shinpei Avatar asked Aug 02 '14 08:08

shinpei


People also ask

Does go compile to binary?

To complete the list, go run compiles your application into a temporary folder, and starts that executable binary. When the app exits, it properly cleans up the temporary files.

Where does go install put binaries?

Much like the previous behaviour of go get , go install places binaries in $GOPATH/bin , or in $GOBIN if set. See the “Setting up your PATH “ section in Installing Go to ensure your PATH is set correctly.

What is a binary in go?

Go Binaries is an open-source server allowing non-Go users to quickly install tools written in Golang, without installing the Go compiler or a package manager — all you need is curl .


2 Answers

Just to make my comment "official":

go build -o atool-cli github.com/you/atool
like image 76
elithrar Avatar answered Oct 26 '22 10:10

elithrar


One way packages structure themselves as a library, and provide main packages is to put their main entrypoints in subdirectories.

You can have a main package in github.com/myaccount/atool/atool-cli, which imports github.com/myaccount/atool and implements func main(). Some packages with multiple commands even have a /cmd/ directory with multiple cli tools that can be built (see camlistore as an example)

like image 9
JimB Avatar answered Oct 26 '22 09:10

JimB