go 11以后启用了go mod功能,用于管理依赖包。
当执行go mod init
生成go.mod
文件之后,golang在运行
、编译
项目的时候,都会检查依赖并下载依赖包。
在启动了go mod
之后,通过go mod
下载的依赖包,不在放在GOPATH/src
中,而是放到GOPATH/pkg/mod
中。
比如我当前的GOPATH=/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", "[email protected]", "password")
s := smtp.NewSMTP("smtp.exmail.qq.com:25", "[email protected]", "password",false,false,false)
log.Println(s.SendMail("[email protected]", "[email protected];[email protected]", "这是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:gProj
- go 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时的参数调用。
升级git
-
macos:
brew upgrade git
-
centos6/7
Remove old git
sudo yum remove git*
centos6:
sudo yum -y install https://centos6.iuscommunity.org/ius-release.rpm
centos7:
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo 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: ��ʹ����Ȩ���¼�������뿴: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256