日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

3.0 go mod之远程仓库搭建-代码示例

發布時間:2023/12/18 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 3.0 go mod之远程仓库搭建-代码示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

注意事項

所謂的遠程倉庫指的是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生效

?

轉載于:https://www.cnblogs.com/perfei/p/11514497.html

總結

以上是生活随笔為你收集整理的3.0 go mod之远程仓库搭建-代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。