go 1.16.4: 用mod管理第三方库

一,查看某个第三方库的可用版本:

此处以gin为例:

liuhongdi@ku:~$ go list -m -versions github.com/gin-gonic/gin
github.com/gin-gonic/gin v1.1.1 v1.1.2 v1.1.3 v1.1.4 v1.3.0 v1.4.0
 v1.5.0 v1.6.0 v1.6.1 v1.6.2 v1.6.3 v1.7.0 v1.7.1 v1.7.2

说明:刘宏缔的go森林是一个专注golang的博客,
          地址:https://blog.csdn.net/weixin_43881017

说明:作者:刘宏缔 邮箱: [email protected]

二,查看本地已下载的版本:

liuhongdi@ku:~$ ls /home/liuhongdi/go/pkg/mod/github.com/gin-gonic
[email protected]  [email protected]

说明:/home/liuhongdi/go/pkg/mod是GOMODCACHE的路径,

           用来保存第三方库

例:

liuhongdi@ku:~$ go env
...
GOMODCACHE="/home/liuhongdi/go/pkg/mod"

三,下载指定版本的第三方库:

说明:在@后面指定版本

liuhongdi@ku:~$ go get github.com/gin-gonic/[email protected]
go: downloading github.com/gin-gonic/gin v1.7.1

查看下载后效果:

liuhongdi@ku:~$ ls /home/liuhongdi/go/pkg/mod/github.com/gin-gonic
[email protected]  [email protected]  [email protected]

四,下载最新版本的第三方库:

liuhongdi@ku:~$ go get -u github.com/gin-gonic/gin
…

查看效果:

liuhongdi@ku:~$ ls /home/liuhongdi/go/pkg/mod/github.com/gin-gonic
[email protected]  [email protected]  [email protected]  [email protected]

五,在项目中使用指定版本的第三方库:

说明:需在项目目录下操作

查看原版本:

liuhongdi@ku:/data/liuhongdi/digv01$ more go.mod
module github.com/liuhongdi/digv01

go 1.16

require (
    github.com/gin-gonic/gin v1.6.3
    github.com/jinzhu/gorm v1.9.16
)

指定使用v1.7.2版本

liuhongdi@ku:/data/liuhongdi/digv01$ go get github.com/gin-gonic/[email protected]
liuhongdi@ku:/data/liuhongdi/digv01$ more go.mod
module github.com/liuhongdi/digv01

go 1.16

require (
    github.com/gin-gonic/gin v1.7.2
    github.com/jinzhu/gorm v1.9.16
)

六,查看指定项目的第三方库的所有依赖:

说明:需在项目目录下操作

liuhongdi@ku:/data/liuhongdi/digv01$ go list -m all
github.com/liuhongdi/digv01
github.com/PuerkitoBio/goquery v1.5.1
github.com/andybalholm/cascadia v1.1.0
...

七,从项目中手动删除一个依赖:

查看go.mod

liuhongdi@ku:/data/liuhongdi/digv01$ more go.mod
module github.com/liuhongdi/digv01

go 1.16

require (
    github.com/gin-gonic/gin v1.7.0
    github.com/jinzhu/gorm v1.9.16
)

删除

# -droprequire: 指定要移除的依赖项

liuhongdi@ku:/data/liuhongdi/digv01$ go mod edit -droprequire=github.com/gin-gonic/gin

查看效果

liuhongdi@ku:/data/liuhongdi/digv01$ more go.mod
module github.com/liuhongdi/digv01

go 1.16

require github.com/jinzhu/gorm v1.9.16

八,在项目中手动添加一个依赖:

说明:需要指定版本

# -require: 指定要添加的依赖项

liuhongdi@ku:/data/liuhongdi/digv01$ go mod edit -require=github.com/gin-gonic/[email protected]

查看效果

liuhongdi@ku:/data/liuhongdi/digv01$ more go.mod
module github.com/liuhongdi/digv01

go 1.16

require (
    github.com/gin-gonic/gin v1.7.1
    github.com/jinzhu/gorm v1.9.16
)

说明:与在项目目录下执行 go  get 命令相同

九,查看go的版本:

liuhongdi@lhdpc:/data/go/mod3$ go version
go version go1.16.4 linux/amd64

 

你可能感兴趣的:(go安装配置,go,gomod,子模块,第三方库,list)