尝试使用了go mod去管理包,按照官方的文档指引https://blog.golang.org/using-go-modules进行实践。
整个过程中出现了几个问题:

  • gomod文件应该放在哪里?
    首先项目得在GOPATH以外的任意目录,再执行 go mod init [根目录名]
    目录组织结构:

     wwwroot-------go.mod
            -------main.go
            -------hello/hello.go
            ------------/hello_test.go
           

    如果用把hello目录里面需要的包拉下来,只需要运行 go [根目录名]/hello

  • 使用go mod 时依赖包下不下来?
    这个是因为默认的地址因为被ban了,所以需要可以用替换的一个代理,可以用这个https://goproxy.io/
    具体可以参考:https://goproxy.io/zh/
    我的环境是win7 + go 1.12,不同操作系统可以参考链接里具体怎么设置,不过因为有git bash,直接用的linux设置方式

    vim /etc/profile
    export GOPROXY=https://goproxy.io
    source /etc/profile

    至此,问题解决。

  • goland设置代理?

设置(ctrl+alt+s)->Go->Go Modules(vgo)->设置proxy为https://goproxy.io->点击Apply。
如果没生效可以就可以重启GoLand即可

标签: go

评论已关闭