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", "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: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

最新文章

  1. Python语言常见异常错误类型
  2. 【转】Eclipse下启动tomcat报错:/bin/bootstrap.jar which is referenced by the classpath, does not exist.
  3. mysql日志的查看与开启
  4. 17.2.1 Replication Implementation Details 复制实现细节:
  5. Btrace是一个实时监控工具
  6. Razor和HtmlHelper的使用意义
  7. 3D VR卡镜的使用方法
  8. 用MPLAB IDE编程时,软件总是弹出一个窗口提示: “the extended cpu mode configuration bit is enabled,but the program that was loaded was not built using extended cpu instructions. therefore,your code may not work properly
  9. java.net.SocketException: Broken pipe 异常可能的原因
  10. 简单的记录,VMware Tools的安装
  11. Hive安装和部署
  12. 【LOJ#2402】[THUPC2017]天天爱射击(整体二分)
  13. 【jdbc】连接数据库从浅入深
  14. Luogu 3793 由乃救爷爷
  15. openstack-KVM管理工具
  16. CAN通信详解
  17. C# 多窗体之间方法调用
  18. 泡泡一分钟:A Multi-Position Joint Particle Filtering Method for Vehicle Localization in Urban Area
  19. 银联卡中关于CVN/CVN2/ICVN的区别
  20. Webstorm使用教程详解

热门文章

  1. ngnix 安装
  2. python字典的内建函数
  3. 【vue系列】Virtual DOM 真的比操作原生 DOM 快吗?
  4. Python自学day-10
  5. Hadoop 学习之路(四)—— Hadoop单机伪集群环境搭建
  6. Python文件中将print的输出内容重定向到变量中
  7. centos7安装apache http server启动失败--Failed to start The Apache HTTP Server.
  8. Java连载3-编译与运行阶段详解&amp;JRE,JDK,JVM关系
  9. Python 3网络爬虫开发实战中文 书籍软件包(原创)
  10. Spring中的配置文件文件位置