wiki
https://github.com/golang/go/wiki/Modules#how-to-prepare-for-a-release
参考
https://blog.csdn.net/benben_2015/article/details/82227338
go mod 之本地仓库搭建
----------------------------------------------------------------------------------------
将当前项目添加到$GOPATH中--这是使用的前提条件
11版本中的临时变量on/off,on表示使用go mod同时禁用$GOPATH
export GO111MODULE=on
export GO111MODULE=off
在各个包下面执行
go mod init
在main方法所在的包下执行
go mod init
修改main程序包下的go.mod,将需要的包添加进来
vim go.mod
module test
require mha v0.0.0
replace mha => ../mha
go 1.12
go.mod说明
--------------------------------------------------
module test中的test是指GOPATH src下的全路径,这里就是$GOPATH/src/test
require mha中的mha也是如此
如果在github上的话,这里的路径将是 github.com/项目名称/包名称
replace 则是使用本地仓库的包,替换指定的包
如果使用了go mod,那么包的引入规则只能全部以该方式进行,不能部分包使用go mod,部分包还使用$GOPATH
export GO111MODULE=off 禁用 go mod后,$GOPATH生效,不需要删除各包下的go.mod文件,原来的项目依然可以运行
再看一个更详细的例子
===========================================================================
GOPATH目录为空
root@black:~# echo $GOPATH
/opt/code/gopath
root@black:~# cd /opt/code/gopath/
root@black:/opt/code/gopath# ls
bin src
root@black:/opt/code/gopath# cd src
root@black:/opt/code/gopath/src# ls
root@black:/opt/code/gopath/src#
mkdir test
vim test/test.go
package main
import(
"fmt"
"time"
)
func test(){
c := make(chan struct{})
go func(){
fmt.Println("我要出去看看园子里的花还活着吗")
time.Sleep(7*time.Second)
c <- struct{}{}
}()
<- c
fmt.Println("这花被别人拿走了,再也看不到它了")
}
func main(){
test()
}
# go run test/test.go
我要出去看看园子里的花还活着吗
这花被别人拿走了,再也看不到它了
上面是GOPATH方式运行的程序,现在以go mod方式运行
打开MOD打开
export GO111MODULE=on
初始化
cd test
go mod init
go: cannot determine module path for source directory /opt/dev/test (outside GOPATH, no import comments)
这里说我们的项目根目录必须位于GOPATH中,那么,我们将项目根目录加入到GOPATH中
export GOPATH=/opt/code/gopath
export GOPATH=$GOPATH:/opt/dev/test
source /etc/profile
# echo $GOPATH
/opt/code/gopath:/opt/dev/test
cd /opt/dev/test
mkdir src
将之前的脚本目录移动过来
再次运行go mod init
root@black:/opt/dev/test/src/test# go mod init
go: creating new go.mod: module test
root@black:/opt/dev/test/src/test# cat go.mod
module test
go 1.12
root@black:/opt/dev/test/src/test# go run test.go
我要出去看看园子里的花还活着吗
这花被别人拿走了,再也看不到它了