package main
import (
"github.com/sirupsen/logrus"
)
func main() {
logrus.Println("hello, gopath mode")
}
package main
import (
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
func main() {
logrus.Println("hello, gopath mode")
logrus.Println(uuid.NewString())
}
PS D:\coder\goprojects\webtest> go get github.com/google/uuid
go get: added github.com/google/uuid v1.3.0
PS D:\coder\goprojects\webtest> go mod tidy
最终go.mod 和 go.sum 都会生成对应的 uuid包信息
go.mod
module github.com/bigwhite/module-mode
go 1.16
require (
github.com/google/uuid v1.3.0 // indirect
github.com/sirupsen/logrus v1.9.0
)
go.sum
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
PS D:\coder\goprojects\webtest> go build main.go
PS D:\coder\goprojects\webtest> .\main.exe
time="2022-11-26T15:47:32+08:00" level=info msg="hello, gopath mode"
time="2022-11-26T15:47:32+08:00" level=info msg=ef0d45f8-1cab-4858-8311-9d5ee8e70ecc
可以使用go命令查看 当前的包的版本
PS D:\coder\goprojects\webtest> go list -m -versions github.com/sirupsen/logrus
github.com/sirupsen/logrus v0.1.0 v0.1.1 v0.2.0 v0.3.0 v0.4.0 v0.4.1 v0.5.0 v0.5.1 v0.6.0 v0.6.1 v0.6.2 v0.6.3 v0.6.4 v0.6.5 v0.6.6 v0.7.0 v0.7.1 v0.7.2 v0.7.3 v0.8.0
v0.8.1 v0.8.2 v0.8.3 v0.8.4 v0.8.5 v0.8.6 v0.8.7 v0.9.0 v0.10.0 v0.11.0 v0.11.1 v0.11.2 v0.11.3 v0.11.4 v0.11.5 v1.0.0 v1.0.1 v1.0.3 v1.0.4 v1.0.5 v1.0.6 v1.1.0 v1.1
.1 v1.2.0 v1.3.0 v1.4.0 v1.4.1 v1.4.2 v1.5.0 v1.6.0 v1.7.0 v1.7.1 v1.8.0 v1.8.1 v1.9.0
PS D:\coder\goprojects\webtest> go get github.com/sirupsen/[email protected]
go get: downgraded github.com/sirupsen/logrus v1.9.0 => v1.8.0
PS D:\coder\goprojects\webtest> go mod edit -require="github.com/sirupsen/[email protected]"
PS D:\coder\goprojects\webtest> go mod tidy
go.mol 文件更新如下
module github.com/bigwhite/module-mode
go 1.16
require (
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.8.0
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
PS D:\coder\goprojects\webtest> go get github.com/sirupsen/[email protected]
go get: upgraded github.com/sirupsen/logrus v1.8.0 => v1.9.0
PS D:\coder\goprojects\webtest> go mod edit -require="github.com/sirupsen/[email protected]"
PS D:\coder\goprojects\webtest> go mod tidy
go.mod 文件如下
module github.com/bigwhite/module-mode
go 1.16
require (
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.9.0
)
package main
import (
_ "github.com/go-redis/redis/v7"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
func main() {
logrus.Println("hello, gopath mode")
logrus.Println(uuid.NewString())
}
redis v7 版本升级 到v8 版本
package main
import (
_ "github.com/go-redis/redis/v8"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
func main() {
logrus.Println("hello, gopath mode")
logrus.Println(uuid.NewString())
}
使用go mod tidy 升级
PS D:\coder\goprojects\webtest> go mod tidy
go: finding module for package github.com/go-redis/redis/v8
go: found github.com/go-redis/redis/v8 in github.com/go-redis/redis/v8 v8.11.5
go.mod 文件
go 1.16
require (
github.com/go-redis/redis/v8 v8.11.5
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.9.0
)
去掉源码中 import 的redis 依赖包
package main
import (
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
func main() {
logrus.Println("hello, gopath mode")
logrus.Println(uuid.NewString())
}
PS D:\coder\goprojects\webtest> go mod tidy
go.mod 中已经自动去掉了 go依赖包路径
module github.com/bigwhite/module-mode
go 1.16
require (
github.com/google/uuid v1.3.0
github.com/sirupsen/logrus v1.9.0
)
go module 中可以处理大多数的依赖包处理问题,一般不会使用vendor 机制去存储 go依赖包信息,但是有的环境问题,无法或者一些不方便访问外部网络 就需要使用到vendor机制
需要在go build 执行后增加 -mod=vendor 在我当前使用的版本默认是使用 go mod 方式编译项目的
PS D:\coder\goprojects\webtest> go build -mod=vendor .\main.go
go: inconsistent vendoring in D:\coder\goprojects\webtest:
github.com/google/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
github.com/sirupsen/[email protected]: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
To ignore the vendor directory, use -mod=readonly or -mod=mod.
To sync the vendor directory, run:
go mod vendor
提示异常没有对应的vendor/modules.txt, 提示我们使用命令 go mod vendor 去构建vendor机制创建vendor 目录并下载源码对应的依赖包文件 以及 modules.txt 记录依赖包 路径以及版本
PS D:\coder\goprojects\webtest> go mod vendor
PS D:\coder\goprojects\webtest> go build -mod=vendor .\main.go
好记性不如烂笔头,本文学自 极客时间 Tony Bai · Go 语言第一课