当前位置:网站首页>Golang Modules
Golang Modules
2022-07-04 09:33:00 【动态一时爽,重构火葬场】
创建新module
在$GOPATH/src之外的文件夹(比如//hello),创建全新的go文件,比如说hello.go。并且让我们为他写个test。运行之后会发现
PASS
ok _/hello
因为我们工作在 $GOPATH和任意模块之外,go 命令不知道当前目录的导入路径(导入路径是标识包的唯一字符串标识)所以根据目录所在位置创建了一个假的导入路径 _/hello
让我们使用go mod init将文件夹初始化为module吧!
go mod init demo.com/hello
go mod init将会写go.mod文件
go.mod仅仅只会出现在模块的根目录。在模块下的子目录包导入路径由模块路径加子目录路径组成。比如说模块下有子目录/world,那么导入路径就为demo.com/hello/world
添加依赖
go命令通过使用go.mod中列出特定依赖模块版本来解析导入。当go.mod未指定时,会自动找到最新依赖,添加到go.mod
当然除了go.mod中列出的直接依赖之外,还有一些间接依赖。比如说rsc.io/quote这个模块还依赖rsc.io/sampler,那么rsc.io/sampler也会下载
可通过
go list -m all列出当前模块所有依赖项
此外,go.sum还记录来依赖模块版本的加密哈希值。但go.sum并不是package-lock.json,只会记录当前模块所有直接间接的依赖,以及校验和。
升级依赖
我们可以通过go get来升级到最新依赖版本。
但有时也会出现不兼容的情况
$ go get rsc.io/sampler
go: finding rsc.io/sampler v1.99.99
go: downloading rsc.io/sampler v1.99.99
go: extracting rsc.io/sampler v1.99.99
$ go test
--- FAIL: TestHello (0.00s)
hello_test.go:8: Hello() = "99 bottles of beer on the wall, 99 bottles of beer, ...", want "Hello, world."
FAIL
exit status 1
FAIL example.com/hello 0.014s
$
此时可以列出所有模块可用tagged版本,然后选择兼容版本下载
$ go list -m -versions rsc.io/sampler
rsc.io/sampler v1.0.0 v1.2.0 v1.2.1 v1.3.0 v1.3.1 v1.99.99
$ go get rsc.io/[email protected]
删除依赖
go mod tidy会清除这些未使用的依赖项
添加依赖的主版本
假设我们要用一个依赖的多个不同的主版本
package hello
import (
"rsc.io/quote"
quoteV3 "rsc.io/quote/v3"
)
func Hello() string {
return quote.Hello()
}
func Proverb() string {
return quoteV3.Concurrency()
}
Go模块的每个不同的版本都使用不同的模块路径。
replace 替换require中声明依赖
module demo
replace outter => /local/outter
require (
outter v1.0.0
)
Ref
- https://go.dev/blog/using-go-modules
- https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/
边栏推荐
- ASP. Net to access directory files outside the project website
- Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
- Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
- 2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
- 2022-2028 global optical transparency industry research and trend analysis report
- PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers
- The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
- Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
- Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
- 自动化的优点有哪些?
猜你喜欢

Sword finger offer 30 contains the stack of Min function
![C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)](/img/89/0f5f4ba07c637b09abe873016cba2d.png)
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)

2022-2028 global strain gauge pressure sensor industry research and trend analysis report

ASP. Net to access directory files outside the project website

Mantis creates users without password options

165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version

回复评论的sql

2022-2028 global small batch batch batch furnace industry research and trend analysis report

How to ensure the uniqueness of ID in distributed environment

QTreeView+自定义Model实现示例
随机推荐
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
How do microservices aggregate API documents? This wave of show~
自动化的优点有哪些?
Four common methods of copying object attributes (summarize the highest efficiency)
Pueue data migration from '0.4.0' to '0.5.0' versions
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
2022-2028 global small batch batch batch furnace industry research and trend analysis report
Are there any principal guaranteed financial products in 2022?
Lauchpad x | MODE
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Rules for using init in golang
UML sequence diagram [easy to understand]
Opencv environment construction (I)
You can see the employment prospects of PMP project management
20220701 barbarat lemma proof
2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
Explanation of closures in golang