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
我要出去看看園子里的花還活著嗎
這花被別人拿走了,再也看不到它了
?