当前位置:网站首页>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/
边栏推荐
- Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
- Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Write a jison parser from scratch (6/10): parse, not define syntax
- 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
- 2022-2028 global industrial gasket plate heat exchanger industry research and trend analysis report
- 技术管理进阶——如何设计并跟进不同层级同学的绩效
- 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
- GoLand environment variable configuration
- Global and Chinese trisodium bicarbonate operation mode and future development forecast report Ⓢ 2022 ~ 2027
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
猜你喜欢
C语言指针经典面试题——第一弹
2022-2028 global seeder industry research and trend analysis report
Nuxt reports an error: render function or template not defined in component: anonymous
Svg image quoted from CodeChina
Hands on deep learning (35) -- text preprocessing (NLP)
技术管理进阶——如何设计并跟进不同层级同学的绩效
2022-2028 global visual quality analyzer industry research and trend analysis report
libmysqlclient.so.20: cannot open shared object file: No such file or directory
2022-2028 global strain gauge pressure sensor industry research and trend analysis report
MySQL foundation 02 - installing MySQL in non docker version
随机推荐
UML sequence diagram [easy to understand]
Get the source code in the mask with the help of shims
What is uid? What is auth? What is a verifier?
Flutter tips: various fancy nesting of listview and pageview
JDBC and MySQL database
Lauchpad X | 模式
PMP registration process and precautions
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
UML 时序图[通俗易懂]
xxl-job惊艳的设计,怎能叫人不爱
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
"How to connect the Internet" reading notes - FTTH
How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
How does idea withdraw code from remote push
How web pages interact with applets
2022-2028 global protein confectionery industry research and trend analysis report
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
Hands on deep learning (35) -- text preprocessing (NLP)
Write a mobile date selector component by yourself