1. package management
go package management
1. history
2. go modules
GOPATH: Unified package storage path
- not support version control of dependency packages
GOPATH mode: use GOPATH to manage
GOPATH: the path
不是使用GOPATH模式就一定是使用GOPATH路径
GOROOT是go installation path,存放Build-in pkgs
GOPATH是 designated workspace
GOROOT 和 GOPATH 不能相同目录,否则包名冲突
GOPATH mode下,工程代码必须放在GOPATH/src
go get:
1. git clone to $GOPATH/src
2. go install (-d 不执行install)
go install:
1. generate executable binary file -> $GOPATH/bin
普通包:编译生成.a结尾的文件放到$GOPATH/pkg
(编译缓存,提升编译速度)
what is executable? has main() function
为什么需要go install?
因为go get要从remote repo,如果想用local repo,就用go install。
1.15版本后go install会再判断没有local时拉取远程
go build
Output compiled files to the current directory
go run
用于测试,compile and run main()
GOPATH directory:
/src: source code
/pkg: compiled files
/bin: compiled .exe
GO MODULES
GO111MODULE = auto -> 根据项目是否在GOPATH dir 判断什么模式
go env -w GO111MODULE = on
如何变成module形式?
1. set GO111MODULE = on (windows)
2. go mod init <name>
(generate go.mod)
go mod tidy
1. 没有的包就下载
2. 多余的包就删除
如果go.mod和go.sum包含某个包,却没有下载到本地?
- go mod download
go mod vendor
go.sum
- 本地缓存有依赖包,计算包的hash并与go.sum对比
- 保证依赖的module version 不会被篡改(not tampered with)
go.sum中记录比go.mod多?
.mod依赖包包含go.mod -> 不记录;不包含 -> 记录indirect dependency
.sum: all
dependency pkg storage:
GOPATH mode: $GOPATH/src
GOMODULE mode: $GOPATH/pkg/mod
2.
标签:GOPATH,sum,基础,Golang,pkg,install,go,mod From: https://www.cnblogs.com/sabertobih/p/17813693.html