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
我要出去看看園子里的花還活着嗎
這花被別人拿走了,再也看不到它了
再看一個稍微復雜的例子,以go1.13為例
=======================================
項目src目錄有以下目錄
common
config
model
tools
main/esql
共五個包,每個包下面都需要執行go mod init
tools中引用了common、config、model包
main/esql引用了tools
本地mod, 版本號的數字,可以隨意寫
common go.mod
module common go 1.13
config go.mod
module config go 1.13
model go.mod
module model go 1.13
tools go.mod
module tools go 1.13 require ( common v0.0.0 config v0.0.9 model v0.0.0 ) replace common => ../common replace config => ../config replace model => ../model
main/esql go.mod
module main/esql go 1.13 require ( common v0.0.0 config v0.0.9 model v0.0.0 tools v0.0.0 ) replace tools => ../../tools replace common => ../../common replace config => ../../config replace model => ../../model
其他go mod相關命令
go clean -modcache
go mod自動下載路徑
----------------------------------------------
# echo $GOPATH
/opt/wks/gopath:/opt/wks/wks_go/9db
# ll /opt/wks/gopath/pkg/mod/cache/download/github.com/baidu/go-lib
total 12
drwxr-xr-x 3 tan tan 4096 11月 28 22:44 ./
drwxr-xr-x 3 tan tan 4096 11月 28 22:44 ../
drwxr-xr-x 2 tan tan 4096 2月 20 14:27 '@v'/
下載編譯他人的gom項目,以gotop項目為例
---------------------------------------------------------------------
go get github.com/cjbassi/gotop
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ echo $GOPATH
/opt/wks/gopath:/opt/wks/wks_go/9db:/opt/wks/wks_go/dbm
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ ll
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ go build -o gotop main.go go: downloading github.com/gizak/termui/v3 v3.0.0 go: downloading github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 go: downloading github.com/shirou/gopsutil v2.18.11+incompatible go: downloading github.com/distatus/battery v0.9.0 go: extracting github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815 go: extracting github.com/distatus/battery v0.9.0 go: extracting github.com/gizak/termui/v3 v3.0.0 go: downloading github.com/mitchellh/go-wordwrap v1.0.0 go: downloading github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d go: downloading github.com/mattn/go-runewidth v0.0.4 go: downloading github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd go: extracting github.com/shirou/gopsutil v2.18.11+incompatible go: extracting github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd go: extracting github.com/mitchellh/go-wordwrap v1.0.0 go: extracting github.com/mattn/go-runewidth v0.0.4 go: extracting github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d go: downloading golang.org/x/sys v0.0.0-20190116161447-11f53e031339 go: extracting golang.org/x/sys v0.0.0-20190116161447-11f53e031339 go: finding github.com/gizak/termui/v3 v3.0.0 go: finding github.com/cjbassi/drawille-go v0.0.0-20190126131713-27dc511fe6fd go: finding github.com/mattn/go-runewidth v0.0.4 go: finding github.com/mitchellh/go-wordwrap v1.0.0 go: finding github.com/nsf/termbox-go v0.0.0-20190121233118-02980233997d go: finding github.com/distatus/battery v0.9.0 go: finding github.com/shirou/gopsutil v2.18.11+incompatible go: finding golang.org/x/sys v0.0.0-20190116161447-11f53e031339 go: finding github.com/docopt/docopt.go v0.0.0-20180111231733-ee0de3bc6815
tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ ll total 4360 drwxr-xr-x 12 tan tan 4096 4月 4 09:57 ./ drwxr-xr-x 3 tan tan 4096 4月 4 09:52 ../ drwxr-xr-x 5 tan tan 4096 4月 4 09:52 assets/ drwxr-xr-x 2 tan tan 4096 4月 4 09:52 build/ -rw-r--r-- 1 tan tan 2532 4月 4 09:52 CHANGELOG.md drwxr-xr-x 2 tan tan 4096 4月 4 09:52 ci/ drwxr-xr-x 2 tan tan 4096 4月 4 09:52 colorschemes/ drwxr-xr-x 8 tan tan 4096 4月 4 09:52 .git/ drwxr-xr-x 3 tan tan 4096 4月 4 09:52 .github/ -rw-r--r-- 1 tan tan 108 4月 4 09:52 .gitignore -rw-r--r-- 1 tan tan 809 4月 4 09:57 go.mod -rw-r--r-- 1 tan tan 4399 4月 4 09:52 go.sum -rwxr-xr-x 1 tan tan 4331298 4月 4 09:57 gotop* -rw-r--r-- 1 tan tan 34520 4月 4 09:52 LICENSE -rw-r--r-- 1 tan tan 10825 4月 4 09:52 main.go -rw-r--r-- 1 tan tan 797 4月 4 09:52 Makefile -rw-r--r-- 1 tan tan 3904 4月 4 09:52 README.md drwxr-xr-x 2 tan tan 4096 4月 4 09:52 scripts/ drwxr-xr-x 2 tan tan 4096 4月 4 09:52 snap/ drwxr-xr-x 6 tan tan 4096 4月 4 09:52 src/ -rw-r--r-- 1 tan tan 747 4月 4 09:52 .travis.yml drwxr-xr-x 5 tan tan 4096 4月 4 09:52 vendor/ tan@db:/opt/wks/gopath/src/github.com/cjbassi/gotop$ ./gotop