当前位置:网站首页>关于Go Modules环境搭建和包管理工具的使用
关于Go Modules环境搭建和包管理工具的使用
2022-07-16 21:58:00 【小哈里】
1 GOPATH
Go的依赖管理主要经历了3个阶段,分别是GOPATH, GoVendor, Go Module。
整个演进路线主要围绕实现两个目标来迭代发展的,分别是1,2
GOPATH是Go语言支持的一个环境变量,value是Go项目的工作区。
目录有以下结构:
src存放Go项目的源码;
pkg:存放编译的中间产物,加快编译速度;
bin:存放Go项目编译生成的二讲制文件.
用gopath依赖管理有哪些弊端呢?相信大家都有了自己的思考结果,来我一起看一下,同一个ke ,有2个版本,A->A468, B-EC0而src下只能有一个版本存在,AB无法保证都能编译通过,也就是gopath管理下, 如果有多个项目依赖该库是同一份代码,不同项目不能依赖同一个库的不同版本,这很显然不能满足我们的项目依赖需求。为了解决这问题,govender出现了。
# 命令行输入如下命令查看Go配置信息
go env

GOARCH 表示目标处理器架构。
GOBIN 表示编译器和链接器的安装位置。
GOOS 表示目标操作系统。
GOPATH 表示当前工作目录。
GOROOT 表示 Go 开发包的安装目录。
如何修改GOPATH
windows默认目录%USERPROFILE%\go
直接在环境变量里增删改查即可。
2 GoVendor
- Vendor是当前项目中的一个目录,其中存放了当前项目的版本。在Vendor胡制下,如果当前项目存在Vendor目录,会优先使用该目录下的依赖,如果依赖不存在,会从(GOHATH中寻找)
3 Go Module
- GoModues是Go语言官方推出的依赖管理系统,解决了之前依赖管理系统存在的诸如无法依赖同一个库的多个版本等问题 go mocdue从Go 11.1开始实验性引入,Go 1.16默认开启;
如何查看Go版本
go version
- 设置go mod和go proxy
go env -w会将配置写到 GOENV=“/Users/youdi/Library/Application Support/go/env”
Go Modules环境配置
go env -w GOBIN=/Users/gwj11/go/bin
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct // 使用七牛云的
- GO111MODULE=off,go命令行将不会支持module功能,寻找依赖包的方式将会沿用旧版本那种通过vendor目录或者GOPATH模式来查找。
GO111MODULE=on,go命令行会使用modules,而一点也不会去GOPATH目录下查找。
当modules功能启用时,依赖包的存放位置变更为$GOPATH/pkg,允许同一个package多个版本并存,且多个项目可以共享缓存的 module
4 一个例子
首先我们需要初始化一个go.mod
go mod init test
然后我们引入模块的时候,以test(初始化时定义)开头,然后接模块路径,比如
import "test/module"
新建一个hello.go文件如下
package main
import "github.com/astaxie/beego"
func main() {
beego.Run()
}
按照过去的做法,要运行hello.go需要执行go get 命令 下载beego包到 $GOPATH/src
现在我们可以直接 go run hello.go
稍等片刻… go 会自动查找代码中的包,下载依赖包,并且把具体的依赖关系和版本写入到go.mod和go.sum文件中。
module Gone
go 1.14
require github.com/gin-gonic/gin v1.6.3
查看go.mod,它会变成这样:
module hello
go 1.12
require github.com/astaxie/beego v1.11.1
- 依赖的包下载到哪里了?还在GOPATH里吗?
不在。使用Go的包管理方式,依赖的第三方包被下载到了$GOPATH/pkg/mod
边栏推荐
- Envoy生命周期管理
- Jedi survive eating chicken 98K does not automatically close the mirror
- Introduction to OOM
- 【STL】模拟实现vector
- 开户需要注意的是什么?请问手机开户股票开户安全吗?
- Pytest interface automated testing framework | confitest Py and @pytest Fixture() combination
- 账号创建+登录+联系表单代码
- Shh! Fishing artifact, don't let the boss know| Real time voice to text, fast time sequence prediction, yolov6 can be used in one line of command to sort out CSV | showmeai Information Daily
- 吃鸡扔掉部分物资 绝地求生
- C# 给图片画 矩形,椭圆形,文字
猜你喜欢

用cmd命令进行磁盘清理(主要是系统盘)

在代码中用YYYY-MM-DD要注意了!

【STL】模拟实现vector

Learn more about Arduino steering gear control library file servo h

响应式表单样式透明设计

7月献礼,买云盘就送特级桂七,仅限2个月,欲购从速

7. Common garbage collectors

10. Find out the JVM operation status

Applet: the picker view selector scrolls quickly. When confirming, the "value displays an error."“
Under dynamic memory management (C language) -- common errors and written examination questions of large factories
随机推荐
最大子数组异或和
15. Sum of three numbers [list < list < integer> > ans, ans.add (arrays.aslist (num[i], num[j], num[k]))]
二分查找题目汇总
Compose 使用Coil加载网络图片
Introduction to OOM
gdb 调试技巧:定位程序卡死问题
Pytest interface automated testing framework | confitest Py and @pytest Fixture() combination
Introduction to the universal theme system of SAP appgyver
Back to the top, scroll bar slowly back to the top
10.10:VectorDraw C# VS VectorDraw WEB/Crack-VectorDraw
7. Common garbage collectors
APP商品详情源数据接口
Pytest interface automation test framework | Interface Association
Build Nightingale cluster monitoring system
Solve the problem that WinDbg cannot load ntdll symbols
Summary of binary search questions
Programming examples of stm32f1 and stm32cubeide-w25q-spi-flash driver
Applet: the picker view selector scrolls quickly. When confirming, the "value displays an error."“
Record of SQL questions of Niuke this week
Over fitting and under fitting