• 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{}{}
        }()
    
        <- c
        fmt.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/main
    
    go 1.12

    重点说明-版本号

    在github有类似下面的话,就在页面上绿色的按钮,点击下载的位置的下面一行,其中这个4166d71就是go mod需要的版本号

    Latest commit4166d7121 minutes ago

    那么对应的require部分可以这么写

    module github.com/2haodb/gomng/mod_test/main
    
    require 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生效

  • 相关阅读:
    网站常见的报错
    数据库的备份与还原
    纵向tab标签切换效果
    CSS3动画 transition 过渡,宽度由中间向两边发生过渡
    移入切换图片路径;移出再切回去
    动态修改手机号
    json基本介绍及使用
    py 封装 操作json 的方法
    通过htmltestrunner模块美化报告
    记:运行测试case遇到的问题!
  • 原文地址:https://www.cnblogs.com/perfei/p/11514497.html
Copyright © 2020-2023  润新知