What does go install do?

前端 未结 2 1523
無奈伤痛
無奈伤痛 2021-01-30 15:38

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

2条回答
  •  有刺的猬
    2021-01-30 16:03

    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
    

提交回复
热议问题