简体   繁体   中英

Installing a package to GOPATH in directory with go.mod

I would like to install a package to GOPATH using go get . This became tricky with go tooling migrated to go mod support.

There is no problem outside of the project directory (=directory with go.mod file). In the project directory, I would expect setting G0111MODULE variable should do the job but it doesn't.

G0111MODULE=off go get <package name>

How I can install a package to GOPATH within a directory containing go.mod file? Thank you.

go env output:

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/zb/Library/Caches/go-build"
GOENV="/Users/zb/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/zb/dev/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.13/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.13/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/zb/dev/mycopmany/links/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/gf/8xg53y0j2h9d3k5g3_vvvw9m0000gn/T/go-build248955952=/tmp/go-build -gno-record-gcc-switches -fno-common"

The variable is named GO111MODULE (with a letter O ), not G0111MODULE (with a number 0 ).

It looks like the OP has two distinct needs:

a. Installing a package to GOPATH in directory with go.mod

(as in the title)

As it is stated in the documentation (source: Deprecation of 'go get' for installing executables - The Go Programming Language ),

Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead.

Thus, running go install (with no arguments, as in this example ) from the directory containing the go.mod file will move the executable to $GOPATH/bin which will allow you to run the executable file from any location — provided $GOPATH/bin is part of the $PATH environment variable.

b. “(...) I want to install go program that would be accessible as a command-line tool.”

(as stated in one of the OP's comments)

Now, to address this specific need, you can add /path/to/project_directory to $PATH

# either by running
echo "/path/to/project_directory:$PATH" > $PATH

# or by putting the following line in `~/.bashrc` file (i.e. `/home/myuser/.bashrc`)
PATH="/path/to/project_directory:$PATH"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM