当执行go mod init生成go.mod文件之后,golang在运行、编译项目的时候,都会检查依赖并下载依赖包。
在启动了go mod之后,通过go mod下载的依赖包,不在放在GOPATH/src中,而是放到GOPATH/pkg/mod中。
比如我当前的GOPATH=http://www.likecs.com/root/go,我在/root/goProjects/下新建了一个项目gProject1,并在项目下编写了一些代码,引用了一些第三方包:
echo $GO111MODULE
auto
mkdir /root/goProjects/gProject1
cd /root/goProjects/gProject1
vi main.go
cat main.go
package main import ( "log" "github.com/toolkits/smtp" ) func main() { //s := smtp.New("smtp.exmail.qq.com:25", "notify@a.com", "password") s := smtp.NewSMTP("smtp.exmail.qq.com:25", "notify@a.com", "password",false,false,false) log.Println(s.SendMail("notify@a.com", "ulric@b.com;rain@c.com", "这是subject", "这是body,<font color=red>red</font>")) }go mod init gProject1
go: creating new go.mod: module gProject1
-cat go.mod
module gProject1 go 1.12 yzc:gProjgo run main.go
如果此时报错: build command-line-arguments: cannot load github.com/toolkits/smtp: cannot find module providing package github.com/toolkits/smtp原因是因为git版本较低,go get 无法通过git下载github.com/toolkits/smtp到指定路径。
你可以手动执行一下go get github.com/toolkits/smtp,发现会报一个类似这样的错误:
# go get github.com/toolkits/smtp go get github.com/toolkits/smtp: git ls-remote -q https://github.com/toolkits/smtp in /root/go/pkg/mod/cache/vcs/7028097e3b6cce3023c34b7ceae3657ef3f2bbb25dec9b4362813d1fadd80297: exit status 129: usage: git ls-remote [--heads] [--tags] [-u <exec> | --upload-pack <exec>] <repository> <refs>...就是git版本太低了,无法支撑go get运行git时的参数调用。
升级gitmacos:
brew upgrade git
centos6/7
Remove old git sudo yum remove git*centos6:
sudo yum -y install https://centos6.iuscommunity.org/ius-release.rpmcentos7:
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpmsudo yum -y install git2u-all
再次执行go run main.go:
go: finding github.com/toolkits/smtp latest go: downloading github.com/toolkits/smtp v0.0.0-20190110072832-af41f29c3d89 go: extracting github.com/toolkits/smtp v0.0.0-20190110072832-af41f29c3d89 2019/07/27 16:15:52 535 Error: ��ʹ����Ȩ���¼�������뿴: ?subtype=1&&id=28&&no=1001256