注意事項
所謂的遠程倉庫指的是github,個人首次使用go mod在其他云倉庫上嘗試,并未成功,這浪費了我近2小時的時間;
如果你是初次嘗試,那么除了github的地址換一下之外,其他的都按照示例操作,比如目錄的創建,這也是我把我的操作步驟一個不拉地貼出來的原因,你只須按著做,必定成功;
如果你沒有引用github上的go模塊,也不打算分享代碼到github,那么go mod對你沒有任何作用,使用GOPATH即可。
?
在github上創建一個倉庫
https://github.com/2haodb/gomng.git
把項目復制到本地,并提交一份代碼上去
cd git clone https://github.com/2haodb/gomng.git cd gomng/ git remote add mng https://github.com/2haodb/gomng.git cp -r /opt/dev/test/src/mod_test/ . git add . git commit -m "1.0.1" git push -u mng master
?
代碼內容
別人向你提到使用GO展示一個東西時,一定要用到GO的一些特性,尤其是面試官讓你用GO寫一段代碼的時侯
root@black:~/gomng/mod_test/main# cd .. root@black:~/gomng/mod_test# ls main pkg1 root@black:~/gomng/mod_test# cd pkg1/ root@black:~/gomng/mod_test/pkg1# cat test.go package pkg1 import("fmt""time" )func Test(){c := make(chan struct{})go func(){fmt.Println("我要出去看看園子里的花還活著嗎")time.Sleep(7*time.Second)c <- struct{}{}}()<- cfmt.Println("這花被別人拿走了,再也看不到它了") }
?
root@black:~/gomng/mod_test/main# cat main.go package main import("github.com/2haodb/gomng/mod_test/pkg1" )func main(){pkg1.Test() }
?
執行go mod
# echo $GOPATH
/opt/code/gopath:/opt/dev/test
export GO111MODULE=on
cd ~/gomng/mod_test/pkg1/ rm -rf go.mod go mod init github.com/2haodb/gomng/mod_test/pkg1
root@black:~/gomng/mod_test/main# go mod init github.com/2haodb/gomng/mod_test/main go: creating new go.mod: module github.com/2haodb/gomng/mod_test/main root@black:~/gomng/mod_test/main# ll total 16 drwxr-xr-x 2 root root 4096 9月 12 18:03 ./ drwxr-xr-x 4 root root 4096 9月 12 17:24 ../ -rw------- 1 root root 54 9月 12 18:03 go.mod -rw-r--r-- 1 root root 99 9月 12 17:31 main.go root@black:~/gomng/mod_test/main# cat go.mod module github.com/2haodb/gomng/mod_test/maingo 1.12
?
重點說明-版本號
在github有類似下面的話,就在頁面上綠色的按鈕,點擊下載的位置的下面一行,其中這個4166d71就是go mod需要的版本號
Latest commit4166d7121 minutes ago
?
那么對應的require部分可以這么寫
module github.com/2haodb/gomng/mod_test/mainrequire github.com/2haodb/gomng/mod_test/pkg1 4166d71 go 1.12
?
在運行程序之后會自動轉化為下面的v版本
root@black:~/gomng/mod_test/main# cat go.mod?
module github.com/2haodb/gomng/mod_test/main
require github.com/2haodb/gomng/mod_test/pkg1 v0.0.0-20190912093654-4166d71402a6
go 1.12
?
運行示例
root@black:~/gomng/mod_test/main# go run main.go go: finding github.com/2haodb/gomng/mod_test/pkg1 4166d71 我要出去看看園子里的花還活著嗎 這花被別人拿走了,再也看不到它了 root@black:~/gomng/mod_test/main# go run main.go 我要出去看看園子里的花還活著嗎 這花被別人拿走了,再也看不到它了
可以看到首次運行的結果與第二次不一樣,這是因為首次運行時go把依賴的模塊下載下來了;
mod自動下載代碼位置
go mod方式運行代碼時自動將依賴的模塊下載到$GOPATH/pkg/mod目錄下,后續運行直接引用mod下的模塊;同時,不會再去$GOPATH/src目錄下找了。
root@black:~# echo $GOPATH /opt/code/gopath:/opt/dev/test root@black:~# ll /opt/code/gopath/pkg/mod/github.com/2haodb/gomng/mod_test total 12 drwxr-xr-x 3 root root 4096 9月 12 17:41 ./ drwxr-xr-x 3 root root 4096 9月 12 17:41 ../ dr-x------ 2 root root 4096 9月 12 17:41 'pkg1@v0.0.0-20190912093654-4166d71402a6'/
?
重新演示一下上面的流程-任意位置
root@black:/tmp# mkdir ccc root@black:/tmp# cd ccc/ root@black:/tmp/ccc# vim main.go root@black:/tmp/ccc# go mod init github.com/2haodb/gomng/mod_test/main go: creating new go.mod: module github.com/2haodb/gomng/mod_test/main root@black:/tmp/ccc# vim go.mod root@black:/tmp/ccc# go run main.go go: finding github.com/2haodb/gomng/mod_test/pkg1 4166d71 我要出去看看園子里的花還活著嗎 這花被別人拿走了,再也看不到它了
main.go與go.mod的內容與之前相同,不同的是主程序的位置變了,
但這沒有關系,這正是go mod的意義所在:你的項目代碼可以在任意位置放置,只須正確引用github的代碼;同時也無須關心依賴包的問題了,因為運行程序時, go自動下載依賴包到本地$GOPATH/pkg/mod目錄。
關閉go mod
export GO111MODULE=off
關閉后,GOPATH生效
?