微服务中碰到 go 1.17 新特性
看文档的笔记
说来巧了,java现在也是1.17(前两天java发布javaSE 17 LTS版本,内部号1.17),今天用vscode配了一下java8(据说市面上大部分等下停留在这个版本,我也不想接触那么多新特性),发现内置的java插件所需要的jdk依赖最少是jdk11,碰巧linux mint自带openjdk11,折腾一通又查了一遍,发现此版本不兼容vscode,直接把它卸载了……不说了,等下单独水一篇visual studio code配置java环境的文章。
——–以上全是水文,下面是正题——-
1.17的release如下:
https://golang.org/doc/go1.17
一个多月前发布以后大致看了一下,主要是此版本来了个尝鲜版的泛型、std库里面usafe包更新、cgo包更新,都是些用不上的功能。
今天折腾微服务的时候,照着官方文档来,安装必要工具出现了错误,引发了我一顿思考
在运行下面的命令过程中报错(安装protoc,解析protobuf的工具)
|
|
报错内容:
1.17 installing executables with 'go get' in module mode is deprecated
查了一下Deprecation of ‘go get’ for installing executables原来是1.17砍了go get的功能
根据
- Deprecation of ‘go get’ for installing executables【网页内容】
go help get
【bash 内的go帮助】
摘抄:
Starting in Go 1.17, installing executables with go get
is deprecated. go install
may be used instead.
In a future Go release, go get
will no longer build packages; it will only be used to add, update, or remove dependencies in go.mod
. Specifically, go get
will act as if the -d
flag were enabled.
To install an executable while ignoring the current module, use go install
with a version suffix like @v1.2.3
or @latest
, as below. When used with a version suffix, go install
does not read or update the go.mod
file in the current directory or a parent directory.
Since modules were introduced, the go get
command has been used both to update dependencies in go.mod
and to install commands. This combination is frequently confusing and inconvenient: in most cases, developers want to update a dependency or install a command but not both at the same time.
Since Go 1.16, go install
can install a command at a version specified on the command line while ignoring the go.mod
file in the current directory (if one exists). go install
should now be used to install commands in most cases.
go get
’s ability to build and install commands is now deprecated, since that functionality is redundant with go install
. Removing this functionality will make go get
faster, since it won’t compile or link packages by default. go get
also won’t report an error when updating a package that can’t be built for the current platform.
The -d flag instructs get not to build or install packages. get will only
update go.mod and download source code needed to build packages.
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’. To install a package
ignoring the current module, use ‘go install’ with an @version suffix like
“@latest” after each argument.
总结以下要点:
- go get 原来有两种功能:改代码、安装二进制文件,现在砍了安装二进制文件的功能,安装二进制只能通过go install进行
- go install 目前可以添加版本号,
go install example.com/cmd@latest
或go install example.com/cmd@1.1.1
- go get -d 参数意思是不安装二进制,1.17以后自动添加-d参数