Golang中`go get -u`和`go get -d`有什么区别?

Golang中go get -ugo get -d有什么区别? 关于 go get 的几个问题

  1. go get -ugo get -d 有什么区别?

  2. 执行以下命令时出现错误:

    $ go get -u github.com/cloudflare/cfssl/cmd/cfssl
    go: downloading github.com/jhump/protoreflect v1.10.1
    go get: installing executables with 'go get' in module mode is deprecated.
    	To adjust and download dependencies of the current module, use 'go get -d'.
    	To install using requirements of the current module, use 'go install'.
    	To install ignoring the current module, use 'go install' with a version,
    	like 'go install example.com/cmd@latest'.
    	For more information, see https://golang.org/doc/go-get-install-deprecation
    	or run 'go help get' or 'go help install'.
    # github.com/zmap/zlint/v3/util
    ../../../pkg/mod/github.com/zmap/zlint/v3@v3.2.0/util/encodings.go:38:27: cannot use atv.Type (type "github.com/zmap/zcrypto/encoding/asn1".ObjectIdentifier) as type "encoding/asn1".ObjectIdentifier in argument to IsNameAttribute
    ../../../pkg/mod/github.com/zmap/zlint/v3@v3.2.0/util/oid.go:138:17: cannot use v.Type (type "github.com/zmap/zcrypto/encoding/asn1".ObjectIdentifier) as type "encoding/asn1".ObjectIdentifier in argument to oid.Equal
    aircrafts-MacBook-Pro:kubernetes ldl$ go get -d github.com/cloudflare/cfssl/cmd/cfssl
    go get: added github.com/cloudflare/cfssl v1.6.1
    go get: upgraded github.com/google/uuid v1.1.2 => v1.2.0
    go get: upgraded github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb => v0.0.0-20170929034955-c48cc78d4826
    

    错误信息为:go get: installing executables with 'go get' in module mode is deprecated. 然后我使用了 go get -d

    $ go get -d github.com/cloudflare/cfssl/cmd/cfssljson
    

    这样安装成功了吗?如何检查? 以及 go get -d 的安装位置在哪里?

  3. 看到 go get: installing executables with 'go get' in module mode is deprecated. 这条信息,我想知道是从哪个 Go 版本开始弃用的。



更多关于Golang中`go get -u`和`go get -d`有什么区别?的实战教程也可以访问 https://www.itying.com/category-94-b0.html

2 回复

你好 @markliao

go get -d 会跳过安装步骤,这就是为什么你不会收到 go get -u 所触发的弃用警告。

关于使用 go get 安装二进制文件的弃用问题,你发布的错误信息中包含了一个链接,解释了其时机和原因。

简而言之:对于二进制文件,请使用 go install

更多关于Golang中`go get -u`和`go get -d`有什么区别?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


1. go get -ugo get -d 的区别

go get -ugo get -dgo get 命令的两个不同选项,主要区别如下:

  • go get -u:更新(upgrade)指定的包及其依赖到最新版本,并编译安装可执行文件。在 Go 1.16 之前,这是安装二进制工具的常用方式。但在模块模式下,该行为已被弃用。
  • go get -d:仅下载(download)指定的包及其依赖到本地缓存($GOPATH/pkg/mod),但不编译安装可执行文件。它用于更新 go.mod 文件中的依赖版本,或预下载依赖。

示例:

# 仅下载依赖到缓存,不安装二进制文件
go get -d github.com/cloudflare/cfssl/cmd/cfssl

# 在 Go 1.16 之前,这会更新并安装二进制文件(现已弃用)
go get -u github.com/cloudflare/cfssl/cmd/cfssl

2. 错误处理与安装检查

错误信息 go get: installing executables with 'go get' in module mode is deprecated. 表示在模块模式下,使用 go get 安装可执行文件已过时。从 Go 1.16 开始,推荐使用 go install 安装二进制工具。

使用 go get -d 后如何检查安装? go get -d 仅下载依赖到模块缓存,不会安装可执行文件。要验证是否下载成功,可检查 go.mod 文件是否更新,或查看缓存目录:

# 检查 go.mod 中是否添加了依赖
cat go.mod | grep cfssl

# 查看依赖是否在缓存中(默认位置:$GOPATH/pkg/mod)
ls $GOPATH/pkg/mod/github.com/cloudflare/cfssl@*

安装位置

  • go get -d 下载的依赖存储在 Go 模块缓存中(路径为 $GOPATH/pkg/mod)。
  • 要安装可执行文件,需使用 go install,默认安装到 $GOPATH/bin(或 $GOBIN):
    # 安装最新版本的 cfssl 工具
    go install github.com/cloudflare/cfssl/cmd/cfssl[@latest](/user/latest)
    
    # 检查是否安装成功
    which cfssl  # 或 cfssl --version
    

3. 弃用时间

go get 在模块模式下安装可执行文件的行为从 Go 1.16 开始被弃用。从该版本起,应使用 go install 安装二进制工具。弃用信息在 Go 1.16 的发布说明中明确提及(参考:https://golang.org/doc/go1.16#go-command)。

解决示例中的错误

示例中的编译错误是由于依赖冲突(zmap/zcrypto 与标准库 encoding/asn1 类型不兼容)。这可能是因为依赖版本不匹配。可尝试更新或降级相关依赖:

# 更新所有依赖到兼容版本
go get -u ./...

# 或手动指定依赖版本
go get github.com/zmap/zlint/v3@v3.1.0

总结:

  • 使用 go get -d 仅下载依赖。
  • 使用 go install <package>[@latest](/user/latest) 安装可执行文件。
  • 从 Go 1.16 开始,避免使用 go get 安装二进制工具。
回到顶部