当前位置:网站首页>Golang Modules
Golang Modules
2022-07-04 09:37: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/
边栏推荐
- MySQL foundation 02 - installing MySQL in non docker version
- C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
- Lauchpad X | 模式
- Svg image quoted from CodeChina
- Markdown syntax
- Four common methods of copying object attributes (summarize the highest efficiency)
- Upgrading Xcode 12 caused Carthage to build cartfile containing only rxswift to fail
- ASP. Net to access directory files outside the project website
- 《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
- Write a jison parser from scratch (4/10): detailed explanation of the syntax format of the jison parser generator
猜你喜欢

HMS core helps baby bus show high-quality children's digital content to global developers

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

Logstack configuration details -- elasticstack (elk) work notes 020

pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.

How do microservices aggregate API documents? This wave of show~

Opencv environment construction (I)

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

Markdown syntax

At the age of 30, I changed to Hongmeng with a high salary because I did these three things

Summary of small program performance optimization practice
随机推荐
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
How to write unit test cases
"How to connect the Internet" reading notes - FTTH
Global and Chinese markets of hemoglobin analyzers in care points 2022-2028: Research Report on technology, participants, trends, market size and share
Opencv environment construction (I)
Problems encountered by scan, scanf and scanln in golang
xxl-job惊艳的设计,怎能叫人不爱
Rules for using init in golang
Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
回复评论的sql
C语言指针经典面试题——第一弹
Write a mobile date selector component by yourself
2022-2028 global visual quality analyzer industry research and trend analysis report
H5 audio tag custom style modification and adding playback control events
mmclassification 标注文件生成