1、go mod init module名, 如:go mod init git.xxx.io/xxx/wallet/common
2、go mod tidy. / 如里以前是用vendor管理项目的则先用go mod vendor把依赖下载到根目录vendor下
3、如有报
go: git.xxx.io/xxx/wallet/common/ripple-sdk/data imports
github.com/willf/bitset: github.com/willf/[email protected]: parsing go.mod:
module declares its path as: github.com/bits-and-blooms/bitset
but was required as: github.com/willf/bitset
则打开github.com/willf/bitset浏览器查看release的相关版本, 然后手工在go.mod手工写入require正确或正常的版本.
4、go build.
5、修改本地项目引用, import 不支持相对路径, 必须从module名 + (目录结构) + 包名 这样的绝对路径
6、对于一些组件不符合命名或已经迁移的可以使用replace命令, 如:
replace golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 => github.com/golang/crypto v0.0.0-20181203042331-505ab145d0a9
注意 => 前后必须要用一个空格
------------------------------------------
开始项目就有vendor的情况:
1、go mod init module名. (会从vendor下载到GOPATH)
2、go mod tidy
3、当项目还有红点时说明没有正常require到模块, 比如版本或源码位置迁移等原因. 这时需要使用上面的3步骤处理
4、处理完红点后, go mod vendor一下, 把mod里的模块下载到项目的vendor中
5、把mod文件里的require部分删掉, (mod兼容了vendor, 如果不用vendor的话, 就要相反把vendor目录删掉)
6、可以到main里启动项目了!
-----------------------------------------
go mod download github.com/shopspring/decimal //生成go.sum记录版本信息
//对于没有引入的模块可以直接使用go get 来下载和引入
go get golang.org/x/crypto
如果上面指令下载不了, 则用3中的该去
xxxxxx@xxxxxx-Pro common % go help mod
Go mod provides access to operations on modules.
Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
Usage:
go mod <command> [arguments]
The commands are:
download download modules to local cache
edit edit go.mod from tools or scripts
graph print module requirement graph
init initialize new module in current directory
tidy add missing and remove unused modules
vendor make vendored copy of dependencies
verify verify dependencies have expected content
why explain why packages or modules are needed
Use "go help mod <command>" for more information about a command.
go <command> [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages