当前位置:网站首页>Golang Modules
Golang Modules
2022-07-04 09:37:00 【Dynamic for a while, reconstructing the crematorium】
Create a new module
stay $GOPATH/src Folder outside ( such as //hello), Create a new go file , for instance hello.go. And let's write him a test. It will be found after running
PASS
ok _/hello
Because we work in $GOPATH
And any module outside ,go The command does not know the import path of the current directory ( The import path is the only string ID that identifies the package ) So a fake import path is created according to the location of the directory _/hello
Let's use go mod init
Initialize the folder to module Well !
go mod init demo.com/hello
go mod init
Will write go.mod file
go.mod It will only appear in the root directory of the module . The subdirectory package import path under the module consists of the module path plus the subdirectory path . For example, there are subdirectories under the module /world, Then the import path is demo.com/hello/world
Add dependency
go Command by using go.mod Specific dependent module versions are listed in to resolve the import . When go.mod Unspecified time , Will automatically find the latest dependency , Add to go.mod
Except, of course, go.mod Beyond the direct dependencies listed in , There are also some indirect dependencies . for instance rsc.io/quote This module also relies on rsc.io/sampler, that rsc.io/sampler Will also download
It can be done by
go list -m all
List all dependencies of the current module
Besides ,go.sum Also record encrypted hash values that depend on the module version . but go.sum Not at all package-lock.json, Only all direct and indirect dependencies of the current module will be recorded , And checksums .
Upgrade dependency
We can go through go get
To upgrade to the latest dependent version .
But sometimes there are incompatibilities
$ 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
$
At this point, you can list all modules available tagged edition , Then select a compatible version to download
$ 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]
Delete dependency
go mod tidy
Will clear these unused dependencies
Add dependent major version
Suppose we want to use multiple different master versions of a dependency
package hello
import (
"rsc.io/quote"
quoteV3 "rsc.io/quote/v3"
)
func Hello() string {
return quote.Hello()
}
func Proverb() string {
return quoteV3.Concurrency()
}
Go Each different version of the module uses a different module path .
replace Replace require Declaration of dependency
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/
边栏推荐
- 2022-2028 research and trend analysis report on the global edible essence industry
- Analysis report on the production and marketing demand and investment forecast of tellurium dioxide in the world and China Ⓣ 2022 ~ 2027
- 什么是权限?什么是角色?什么是用户?
- ArrayBuffer
- 浅谈Multus CNI
- 2022-2028 global small batch batch batch furnace industry research and trend analysis report
- C语言指针面试题——第二弹
- Sword finger offer 30 contains the stack of Min function
- Ultimate bug finding method - two points
- After unplugging the network cable, does the original TCP connection still exist?
猜你喜欢
Daughter love in lunch box
PHP book borrowing management system, with complete functions, supports user foreground management and background management, and supports the latest version of PHP 7 x. Database mysql
2022-2028 global special starch industry research and trend analysis report
Hands on deep learning (34) -- sequence model
How web pages interact with applets
Nuxt reports an error: render function or template not defined in component: anonymous
JDBC and MySQL database
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
IIS configure FTP website
el-table单选并隐藏全选框
随机推荐
Markdown syntax
How do microservices aggregate API documents? This wave of show~
Explanation of for loop in golang
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
2022-2028 research and trend analysis report on the global edible essence industry
Launchpad x | mode
Summary of small program performance optimization practice
AMLOGIC gsensor debugging
Investment analysis and prospect prediction report of global and Chinese high purity tin oxide Market Ⓞ 2022 ~ 2027
Global and Chinese markets of water heaters in Saudi Arabia 2022-2028: Research Report on technology, participants, trends, market size and share
Development trend and market demand analysis report of high purity tin chloride in the world and China Ⓔ 2022 ~ 2027
Opencv environment construction (I)
PMP registration process and precautions
ASP. Net to access directory files outside the project website
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
什么是uid?什么是Auth?什么是验证器?
ArrayBuffer
Trees and graphs (traversal)
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Write a jison parser from scratch (3/10): a good beginning is half the success -- "politics" (Aristotle)