当前位置:网站首页>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/
边栏推荐
- In the case of easyUI DataGrid paging, click the small triangle icon in the header to reorder all the data in the database
- Summary of the most comprehensive CTF web question ideas (updating)
- 自动化的优点有哪些?
- Svg image quoted from CodeChina
- Hands on deep learning (33) -- style transfer
- "How to connect the network" reading notes - Web server request and response (4)
- How do microservices aggregate API documents? This wave of show~
- libmysqlclient.so.20: cannot open shared object file: No such file or directory
- 百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
- PHP is used to add, modify and delete movie information, which is divided into foreground management and background management. Foreground users can browse information and post messages, and backgroun
猜你喜欢
C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area
Svg image quoted from CodeChina
2022-2028 global intelligent interactive tablet industry research and trend analysis report
HMS core helps baby bus show high-quality children's digital content to global developers
How to batch change file extensions in win10
SSM online examination system source code, database using mysql, online examination system, fully functional, randomly generated question bank, supporting a variety of question types, students, teache
Some points needing attention in PMP learning
C语言指针经典面试题——第一弹
2022-2028 global edible probiotic raw material industry research and trend analysis report
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
随机推荐
lolcat
HMS core helps baby bus show high-quality children's digital content to global developers
Sword finger offer 30 contains the stack of Min function
Reading notes on how to connect the network - hubs, routers and routers (III)
Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
浅谈Multus CNI
2022-2028 global probiotics industry research and trend analysis report
Tkinter Huarong Road 4x4 tutorial II
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
Ultimate bug finding method - two points
自动化的优点有哪些?
2022-2028 global protein confectionery industry research and trend analysis report
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
How to ensure the uniqueness of ID in distributed environment
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
About the for range traversal operation in channel in golang
Write a jison parser (7/10) from scratch: the iterative development process of the parser generator 'parser generator'
Flutter tips: various fancy nesting of listview and pageview
How web pages interact with applets
PHP personal album management system source code, realizes album classification and album grouping, as well as album image management. The database adopts Mysql to realize the login and registration f