How can I install a package with go get?

huangapple go评论99阅读模式
英文:

How can I install a package with go get?

问题

我想将GitHub上的包安装到我的$GOPATH中,我尝试了以下命令:

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

该仓库在这里

英文:

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.

答案1

得分: 166

> 命令 go
>
> 下载和安装包及其依赖项
>
> 用法:
>
> go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [构建标志] [包]
>
> Get 命令会下载由导入路径指定的包以及它们的依赖项。然后,它会像 'go install' 一样安装这些包。
>
> -d 标志指示 get 在下载完包后停止,也就是说,它不会安装这些包。
>
> -f 标志只在设置了 -u 标志时有效,它强制 get -u 不验证每个包是否已从其导入路径所指示的源代码控制存储库中检出。如果源代码是原始代码的本地分支,这将很有用。
>
> -fix 标志指示 get 在解析依赖项或构建代码之前对下载的包运行修复工具。
>
> -insecure 标志允许使用不安全的协议(如 HTTP)从存储库获取和解析自定义域名。请谨慎使用。
>
> -t 标志指示 get 还要下载构建指定包所需的测试包。
>
> -u 标志指示 get 使用网络更新指定的包及其依赖项。默认情况下,get 使用网络检出缺失的包,但不使用网络查找现有包的更新。
>
> -v 标志启用详细的进度和调试输出。
>
> Get 命令还接受构建标志来控制安装。请参阅 'go help build'。
>
> 在检出新包时,get 会创建目标目录 GOPATH/src/。如果 GOPATH 包含多个条目,则 get 使用第一个条目。有关详细信息,请参阅:'go help gopath'。
>
> 在检出或更新包时,get 会查找与本地安装的 Go 版本匹配的分支或标签。最重要的规则是,如果本地安装运行的是版本 "go1",get 会搜索名为 "go1" 的分支或标签。如果不存在这样的版本,则会检索包的默认分支。
>
> 当 go get 检出或更新 Git 存储库时,它还会更新存储库引用的任何 git 子模块。
>
> Get 永远不会检出或更新存储在 vendor 目录中的代码。
>
> 有关指定包的更多信息,请参阅 'go help packages'。
>
> 有关 go get 如何查找要下载的源代码的更多信息,请参阅 'go help importpath'。
>
> 此文本描述了使用 GOPATH 管理源代码和依赖项时 get 命令的行为。如果 go 命令以模块感知模式运行,get 的标志和效果的详细信息将发生变化,'go help get' 也会有所不同。请参阅 'go help modules' 和 'go help module-get'。
>
> 另请参阅:go build、go install、go clean。


例如,显示详细输出,

$ go get -v github.com/capotej/groupcache-db-experiment/...
github.com/capotej/groupcache-db-experiment (下载)
github.com/golang/groupcache (下载)
github.com/golang/protobuf (下载)
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
$ 
英文:

> 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/<import-path>. 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
$ 

答案2

得分: 125

首先,我们需要设置GOPATH。

$GOPATH 是一个由环境变量指定的文件夹(或一组文件夹)。我们必须注意,这不是Go安装的 $GOROOT 目录。

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

我们在我们的计算机上使用 ~/gocode 路径来存储我们应用程序及其依赖的源代码。GOPATH 目录还将存储其包的二进制文件。

然后检查Go环境。

您的系统必须有 $GOPATH$GOROOT,以下是我的环境:

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"

现在,您可以运行以下命令来下载Go包:

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

go get 命令会下载并安装指定导入路径的包,以及它们的依赖项。更多详细信息可以参考这里

英文:

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=&quot;amd64&quot;
GOBIN=&quot;&quot;
GOCHAR=&quot;6&quot;
GOEXE=&quot;&quot;
GOHOSTARCH=&quot;amd64&quot;
GOHOSTOS=&quot;linux&quot;
GOOS=&quot;linux&quot;
GOPATH=&quot;/home/elpsstu/gocode&quot;
GORACE=&quot;&quot;
GOROOT=&quot;/home/pravin/go&quot;
GOTOOLDIR=&quot;/home/pravin/go/pkg/tool/linux_amd64&quot;
CC=&quot;gcc&quot;
GOGCCFLAGS=&quot;-fPIC -m64 -pthread -fmessage-length=0&quot;
CXX=&quot;g++&quot;
CGO_ENABLED=&quot;1&quot;

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.

答案3

得分: 85

请注意,自Go 1.17起,使用go get安装软件包已被弃用。在将来的版本中,默认情况下将启用-d标志,并且go get仅用于调整当前模块的依赖关系。要使用当前模块的依赖关系安装软件包,请使用go install

这个“将来的版本”是指Go 1.18,在发布说明中提到:

go get不再在模块感知模式下构建或安装软件包。go get现在专门用于调整go.mod中的依赖关系。实际上,-d标志始终处于启用状态。

-d标志指示go get仅下载软件包而不安装它们。)

改用go install

# 安装程序的最新版本,忽略当前目录中的go.mod(如果有)。
$ go install golang.org/x/tools/gopls@latest

# 安装程序的特定版本。
$ go install golang.org/x/tools/gopls@v0.6.4

# 在当前目录的模块中选择的版本安装程序。
$ go install golang.org/x/tools/gopls

# 安装目录中的所有程序。
$ go install ./cmd/...

要安装其他任何软件包,只需执行以下操作:

go install <package_name>@<version>

(您可以省略版本后缀@<version>,例如@latest

Go 1.18发布说明还提到,go getGO111MODULE=off的情况下将像以前一样工作。然而,在2022年,您应该转移到模块并改用go install

英文:

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:

> shell
&gt; # Install the latest version of a program,
&gt; # ignoring go.mod in the current directory (if any).
&gt; $ go install golang.org/x/tools/gopls@latest
&gt;
&gt; # Install a specific version of a program.
&gt; $ go install golang.org/x/tools/gopls@v0.6.4
&gt;
&gt; # Install a program at the version selected by the module in the current directory.
&gt; $ go install golang.org/x/tools/gopls
&gt;
&gt; # Install all programs in a directory.
&gt; $ go install ./cmd/...
&gt;

To install any other package, simply:

go install &lt;package_name&gt;@&lt;version&gt;

(you may omit the version suffix @&lt;version&gt;, e.g. @latest)

<hr>

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.

答案4

得分: -3

从控制台运行它

go mod download
英文:

Run it from console

go mod download

huangapple
  • 本文由 发表于 2015年5月18日 12:32:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/30295146.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定