go get vs go install on go.sum file

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

go get vs go install on go.sum file

问题

我正在使用go 1.20.3版本。

我刚刚使用go get和go install安装了以下包:

go get -v github.com/mactsouk/go/simpleGitHub
go install github.com/mactsouk/go/simpleGitHub

我的go.sum文件中有以下内容:

github.com/mactsouk/go v0.0.0-20180603081621-6a282087f7bd h1:TqJGx/jAxlJ3RNL7pS7XZQlvtH8rL/dUsA8wPe9W4y0=
github.com/mactsouk/go v0.0.0-20180603081621-6a282087f7bd/go.mod h1:TRtlPc1xi1zOQdbA/cIxGdS+fCaIZDQUpMrFlET5dbI=

我的go.mod文件中有以下内容:

module calculator

go 1.20

require github.com/mactsouk/go v0.0.0-20180603081621-6a282087f7bd // indirect

在我的$Home/go/pkg/mod/github.com/mactsouk/目录下,有一个名为go@v0.0.0-20180603081621-6a282087f7bd的文件夹。

在$Home/go/bin/目录下没有任何文件,只有两个文件夹:

drwxr-xr-x 4 user staff 128 Jun 26 23:37 .
drwxr-xr-x 4 user staff 128 Jun 26 23:35 ..
-rwxr-xr-x 1 user staff 3410064 Jun 26 23:35 go-outline
-rwxr-xr-x 1 user staff 28237216 Jun 26 23:37 gopls

为什么go install没有复制simpleGitHub模块的二进制文件?

为什么我的go.sum文件中有两次出现这个模块的记录?go get和go install之间有什么区别?

英文:

I'm using go 1.20.3

I've just install this packages with go get and go install

go get -v github.com/mactsouk/go/simpleGitHub
go install  github.com/mactsouk/go/simpleGitHub

what i have on my go.sum file is:

github.com/mactsouk/go v0.0.0-20180603081621-6a282087f7bd h1:TqJGx/jAxlJ3RNL7pS7XZQlvtH8rL/dUsA8wPe9W4y0=
github.com/mactsouk/go v0.0.0-20180603081621-6a282087f7bd/go.mod h1:TRtlPc1xi1zOQdbA/cIxGdS+fCaIZDQUpMrFlET5dbI=

and on the go.mod file is:

module calculator

go 1.20

require github.com/mactsouk/go v0.0.0-20180603081621-6a282087f7bd // indirect

aod on my $Home/go/pkg/mod/github.com/mactsouk/

dr-xr-xr-x  5 user  staff  160 Jul  4 18:42 go@v0.0.0-20180603081621-6a282087f7bd

and nothing on $Home/go/bin/ but two files, why go install did not copied binary files for simpleGitHub module ?

drwxr-xr-x  4 user  staff       128 Jun 26 23:37 .
drwxr-xr-x  4 user  staff       128 Jun 26 23:35 ..
-rwxr-xr-x  1 user  staff   3410064 Jun 26 23:35 go-outline
-rwxr-xr-x  1 user  staff  28237216 Jun 26 23:37 gopls

there are differences between go get and go install ? why i have on go.sum files this module twice ?

答案1

得分: 5

go getgo install 是 Go 语言中的两个不同命令:

go get 用于从远程仓库检索和下载包及其依赖项。它会更新 go.mod 和 go.sum 文件,记录下载包的版本。如果包已经存在,go get 会将其更新到最新版本。

go install 用于编译并安装包到你的项目的 GOPATHGOBIN 中。它不会更新 go.mod 或 go.sum 文件。相反,它使用这些文件中的信息来确定使用的依赖项的正确版本。

在你的情况下,当你运行 go get 时,它会下载并安装包 github.com/mactsouk/go/simpleGitHub 及其依赖项。这个操作会更新 go.mod 和 go.sum 文件,记录下载包的版本。

当你随后运行 go install 同一个包时,它不需要再次下载包,因为它已经存在于你的本地 Go 模块缓存中。因此,go install 使用缓存中的现有包及其版本,并且不会修改 go.mod 或 go.sum 文件。

你在 go.sum 文件中看到模块列出两次的原因是因为它包含模块版本 (v0.0.0-20180603081621-6a282087f7bd) 和相应的带有校验和的 go.mod 文件。这是正常行为,确保依赖项的完整性。

总结一下,go get 和 go install 有不同的用途,go.sum 文件中模块的重复是正常的。

英文:

go get vs go install

The go get and go install commands in Go serve different purposes:

go get is used to retrieve and download packages and their dependencies from remote repositories. It updates the go.mod and go.sum files with the versions of the downloaded packages. If the package is already present, go get will update it to the latest version.

go install compiles and installs the package in your project's GOPATH or GOBIN. It does not update the go.mod or go.sum files. Instead, it uses the information in those files to determine the correct versions of the dependencies to use.

In your case, when you ran go get, it downloaded and installed the package github.com/mactsouk/go/simpleGitHub and its dependencies. This action updated the go.mod and go.sum files with the versions of the downloaded packages.

When you subsequently ran go install for the same package, it didn't need to download the package again because it was already present in your local Go module cache. Therefore, go install used the existing package and its version from the cache, and it didn't modify the go.mod or go.sum files.

The reason you see the module listed twice in the go.sum file is because it contains both the module version (v0.0.0-20180603081621-6a282087f7bd) and its corresponding go.mod file with a checksum. This is the expected behavior and ensures the integrity of the dependencies.

To summarize, go get and go install have different purposes, and the duplication of the module in the go.sum file is normal.

huangapple
  • 本文由 发表于 2023年7月5日 05:26:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76615942.html
匿名

发表评论

匿名网友

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

确定