重庆做网站个人,专业做网站公司哪家好,青岛关键词排名提升,wordpress客户端配置注意事项 所谓的远程仓库指的是github#xff0c;个人首次使用go mod在其他云仓库上尝试#xff0c;并未成功#xff0c;这浪费了我近2小时的时间#xff1b; 如果你是初次尝试#xff0c;那么除了github的地址换一下之外#xff0c;其他的都按照示例操作#xff0c;比如… 注意事项 所谓的远程仓库指的是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写一段代码的时侯 rootblack:~/gomng/mod_test/main# cd ..
rootblack:~/gomng/mod_test# ls
main pkg1
rootblack:~/gomng/mod_test# cd pkg1/
rootblack:~/gomng/mod_test/pkg1# cat test.go
package pkg1
import(fmttime
)func Test(){c : make(chan struct{})go func(){fmt.Println(我要出去看看园子里的花还活着吗)time.Sleep(7*time.Second)c - struct{}{}}()- cfmt.Println(这花被别人拿走了再也看不到它了)
} rootblack:~/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 GO111MODULEon cd ~/gomng/mod_test/pkg1/
rm -rf go.mod
go mod init github.com/2haodb/gomng/mod_test/pkg1 rootblack:~/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
rootblack:~/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
rootblack:~/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版本 rootblack:~/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 运行示例 rootblack:~/gomng/mod_test/main# go run main.go
go: finding github.com/2haodb/gomng/mod_test/pkg1 4166d71
我要出去看看园子里的花还活着吗
这花被别人拿走了再也看不到它了
rootblack:~/gomng/mod_test/main# go run main.go
我要出去看看园子里的花还活着吗
这花被别人拿走了再也看不到它了 可以看到首次运行的结果与第二次不一样这是因为首次运行时go把依赖的模块下载下来了 mod自动下载代码位置 go mod方式运行代码时自动将依赖的模块下载到$GOPATH/pkg/mod目录下后续运行直接引用mod下的模块同时不会再去$GOPATH/src目录下找了。 rootblack:~# echo $GOPATH
/opt/code/gopath:/opt/dev/test
rootblack:~# 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 pkg1v0.0.0-20190912093654-4166d71402a6/ 重新演示一下上面的流程-任意位置 rootblack:/tmp# mkdir ccc
rootblack:/tmp# cd ccc/
rootblack:/tmp/ccc# vim main.go
rootblack:/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
rootblack:/tmp/ccc# vim go.mod rootblack:/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 GO111MODULEoff 关闭后GOPATH生效 转载于:https://www.cnblogs.com/perfei/p/11514497.html