Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does go install do?

Tags:

go

The docs say nothing about what build vs install does

My expectation was that it's like make install; i.e. it takes the compiled stuff and puts in its final location (/usr/local/bin/my_new_toy or whatever) but it seems that it puts things in GOROOT/bin

Can I tell go to do a make install - i.e. put things elsewhere? Or do I just write a makefile (please tell me no)?

like image 568
pm100 Avatar asked Jun 05 '14 20:06

pm100


People also ask

Where does go install stuff?

go install in Go 1.16 Alternatively, the special latest version can be used to install the latest release. Much like the previous behaviour of go get , go install places binaries in $GOPATH/bin , or in $GOBIN if set.

How do you use GO install instead of go get?

go install , with or without a version suffix (as described above), is now the recommended way to build and install packages in module mode. go get should be used with the -d flag to adjust the current module's dependencies without building packages, and use of go get to build and install packages is deprecated.

How do I install go files?

Add the Go install directory to your system's shell path. That way, you'll be able to run your program's executable without specifying where the executable is. Once you've updated the shell path, run the go install command to compile and install the package. Run your application by simply typing its name.

What happens when you run go build?

go build builds the command and leaves the result in the current working directory. Above program will be able to turn into an executable file that will always print out “Hello World”. If we want our program to run again, we don't have to compile the program again, we simply run the executable file.


2 Answers

go build vs go install:

go build just compiles the executable file and moves it to the destination. go install does a little bit more. It moves the executable file to $GOPATH/bin and caches all non-main packages which are imported to $GOPATH/pkg. The cache will be used during the next compilation provided the source did not change yet.


A package tree after go build and go install:

. ├── bin │   └── hello  # by go install └── src      └── hello         ├── hello  # by go build         └── hello.go 

More detailed information.

like image 73
Benyamin Jafari Avatar answered Oct 01 '22 11:10

Benyamin Jafari


If you want binary files to go to a specific location, you can use the environment variable GOBIN :

The bin/ directory holds compiled commands. Each command is named for its source directory, but only the final element, not the entire path. That is, the command with source in DIR/src/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The foo/ is stripped so that you can add DIR/bin to your PATH to get at the installed commands. If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin.

Source : http://golang.org/cmd/go/#hdr-GOPATH_environment_variable

GOBIN=/usr/local/bin/ go install 

If you want per-project bin/ directory then you can simply append your project path to GOPATH, however you must have your code under $project-path/src/ and go install will put all the binaries in $project-path/bin.

export GOPATH=/dir1:/dir2:/dir3 

If GOBIN is not set, binaries from /dir1/src end up in /dir1/bin, binaries from /dir2/src end up in /dir2/bin, and so on (and binaries from $GOROOT/src end up in $GOROOT/bin).

Source : https://groups.google.com/forum/#!topic/golang-nuts/-mN8R_Fx-7M

And you can also just use (thanks JimB):

go build -o /path/binary-name 
like image 39
OneOfOne Avatar answered Oct 01 '22 13:10

OneOfOne