go mod 模式下安装cobra
只有指明版本号才能成功安装
➜ ~ go get -u github.com/spf13/cobra/cobra@v1.0.0
go: finding github.com/spf13/cobra/cobra v1.0.0
go: finding github.com v1.0.0
go: finding github.com/spf13 v1.0.0
go: extracting github.com/spf13/viper v1.4.0
go: downloading github.com/mitchellh/go-homedir v1.1.0
go: extracting github.com/spf13/pflag v1.0.3
go: extracting github.com/spf13/afero v1.1.2
go: extracting github.com/spf13/cast v1.3.0
go: extracting github.com/mitchellh/go-homedir v1.1.0
go: downloading golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
go: extracting golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
go: finding github.com/pelletier/go-toml v1.8.0
go: finding github.com/magiconair/properties v1.8.2
go: finding golang.org/x/sys latest
go: finding github.com/spf13/viper v1.7.1
go: finding github.com/mitchellh/mapstructure v1.3.3
go: finding github.com/spf13/afero v1.3.4
go: downloading github.com/spf13/viper v1.7.1
go: extracting github.com/spf13/viper v1.7.1
go: downloading github.com/magiconair/properties v1.8.2
go: downloading github.com/pelletier/go-toml v1.8.0
go: downloading github.com/mitchellh/mapstructure v1.3.3
go: downloading github.com/spf13/afero v1.3.4
go: downloading golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8
go: extracting github.com/mitchellh/mapstructure v1.3.3
go: extracting github.com/pelletier/go-toml v1.8.0
go: extracting github.com/magiconair/properties v1.8.2
go: extracting github.com/spf13/afero v1.3.4
go: extracting golang.org/x/sys v0.0.0-20200824131525-c12d262b63d8
go: finding gopkg.in/ini.v1 v1.60.1
go: downloading gopkg.in/ini.v1 v1.60.1
go: extracting gopkg.in/ini.v1 v1.60.1
有了 go get 为什么还要 go install
-
概述
玩 Go 的同学应该都见过这两条命令:go get 和 go install,他们功能类似,那么为什么要有这两条命令呢?本文将对他们的差异进行一个介绍。 -
两条命令的作用
go get 的作用可以分为这么几个步骤:
将源代码下载到本地, 编译下载的代码,并且安装到 GOPATH 中
这里和 go install 的区别在会不会下载,所以 go install 的功能为:
编译指定的代码,然后安装到 GOPATH 中
- 常用模式
所以在很多时候,一个常用的操作模式是:
通过 go get -d 下载模块代码
修改下载下来的代码
通过 go install 安装
- 其他说明
go get 还通常用于在 Go Module 中添加依赖使用
go get 的代码被下载到 $GOPATH/pkg/mod 目录下