How can I install a package with go get?

GoGo Toolchain

Go Problem Overview


I want to install packages from github to my $GOPATH, I have tried this:

go get github.com:capotej/groupcache-db-experiment.git

the repository is here.

Go Solutions


Solution 1 - Go

> Command go > > Download and install packages and dependencies > > Usage: > > go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages] > > Get downloads the packages named by the import paths, along with their > dependencies. It then installs the named packages, like 'go install'. > > The -d flag instructs get to stop after downloading the packages; that > is, it instructs get not to install the packages. > > The -f flag, valid only when -u is set, forces get -u not to verify > that each package has been checked out from the source control > repository implied by its import path. This can be useful if the > source is a local fork of the original. > > The -fix flag instructs get to run the fix tool on the downloaded > packages before resolving dependencies or building the code. > > The -insecure flag permits fetching from repositories and resolving > custom domains using insecure schemes such as HTTP. Use with caution. > > The -t flag instructs get to also download the packages required to > build the tests for the specified packages. > > The -u flag instructs get to use the network to update the named > packages and their dependencies. By default, get uses the network to > check out missing packages but does not use it to look for updates to > existing packages. > > The -v flag enables verbose progress and debug output. > > Get also accepts build flags to control the installation. See 'go help > build'. > > When checking out a new package, get creates the target directory > GOPATH/src/. If the GOPATH contains multiple entries, get > uses the first one. For more details see: 'go help gopath'. > > When checking out or updating a package, get looks for a branch or tag > that matches the locally installed version of Go. The most important > rule is that if the local installation is running version "go1", get > searches for a branch or tag named "go1". If no such version exists it > retrieves the default branch of the package. > > When go get checks out or updates a Git repository, it also updates > any git submodules referenced by the repository. > > Get never checks out or updates code stored in vendor directories. > > For more about specifying packages, see 'go help packages'. > > For more about how 'go get' finds source code to download, see 'go > help importpath'. > > This text describes the behavior of get when using GOPATH to manage > source code and dependencies. If instead the go command is running in > module-aware mode, the details of get's flags and effects change, as > does 'go help get'. See 'go help modules' and 'go help module-get'. > > See also: go build, go install, go clean.


For example, showing verbose output,

$ go get -v github.com/capotej/groupcache-db-experiment/...
github.com/capotej/groupcache-db-experiment (download)
github.com/golang/groupcache (download)
github.com/golang/protobuf (download)
github.com/capotej/groupcache-db-experiment/api
github.com/capotej/groupcache-db-experiment/client
github.com/capotej/groupcache-db-experiment/slowdb
github.com/golang/groupcache/consistenthash
github.com/golang/protobuf/proto
github.com/golang/groupcache/lru
github.com/capotej/groupcache-db-experiment/dbserver
github.com/capotej/groupcache-db-experiment/cli
github.com/golang/groupcache/singleflight
github.com/golang/groupcache/groupcachepb
github.com/golang/groupcache
github.com/capotej/groupcache-db-experiment/frontend
$ 

Solution 2 - Go

First, we need GOPATH

The $GOPATH is a folder (or set of folders) specified by its environment variable. We must notice that this is not the $GOROOT directory where Go is installed.

export GOPATH=$HOME/gocode
export PATH=$PATH:$GOPATH/bin

We used ~/gocode path in our computer to store the source of our application and its dependencies. The GOPATH directory will also store the binaries of their packages.

Then check Go env

You system must have $GOPATH and $GOROOT, below is my Env:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/elpsstu/gocode"
GORACE=""
GOROOT="/home/pravin/go"
GOTOOLDIR="/home/pravin/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

Now, you run download go package:

go get [-d] [-f] [-fix] [-t] [-u] [build flags] [packages]

Get downloads and installs the packages named by the import paths, along with their dependencies. For more details you can look here.

Solution 3 - Go

Note that since Go 1.17 installing packages with go get is deprecated:

> Building and installing packages with get is deprecated. In a future release, the -d flag will be enabled by default, and go get will be only be used to adjust dependencies of the current module. To install a package using dependencies from the current module, use go install.

This "future release" is Go 1.18, as mentioned in the release notes:

> go get no longer builds or installs packages in module-aware mode. go get is now dedicated to adjusting dependencies in go.mod. Effectively, the -d flag is always enabled.

(The -d flag instructs go get to only download packages and not install them.)

Use go install instead:

# Install the latest version of a program,
# ignoring go.mod in the current directory (if any).
$ go install golang.org/x/tools/gopls@latest

# Install a specific version of a program.
$ go install golang.org/x/tools/gopls@v0.6.4

# Install a program at the version selected by the module in the current directory.
$ go install golang.org/x/tools/gopls

# Install all programs in a directory.
$ go install ./cmd/...


The Go 1.18 release notes also mention that go get will work as before with GO111MODULE=off. However in 2022 you should definitely migrate to modules and use go install instead.

Solution 4 - Go

Run it from console

go mod download

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrogerView Question on Stackoverflow
Solution 1 - GopeterSOView Answer on Stackoverflow
Solution 2 - GoPravin MishraView Answer on Stackoverflow
Solution 3 - GoblackgreenView Answer on Stackoverflow
Solution 4 - Godılo sürücüView Answer on Stackoverflow