当前位置:网站首页>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 allList 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/
边栏推荐
- 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
- Log cannot be recorded after log4net is deployed to the server
- Golang 类型比较
- [on February 11, 2022, the latest and most fully available script library collection of the whole network, a total of 23]
- 2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
- Some points needing attention in PMP learning
- At the age of 30, I changed to Hongmeng with a high salary because I did these three things
- Report on investment analysis and prospect trend prediction of China's MOCVD industry Ⓤ 2022 ~ 2028
- Daughter love: frequency spectrum analysis of a piece of music
- Problems encountered by scan, scanf and scanln in golang
猜你喜欢

C # use gdi+ to add text to the picture and make the text adaptive to the rectangular area

Sword finger offer 30 contains the stack of Min function

AMLOGIC gsensor debugging

直方图均衡化

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

Hands on deep learning (34) -- sequence model

Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310

GoLand environment variable configuration

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

2022-2028 global gasket plate heat exchanger industry research and trend analysis report
随机推荐
Lauchpad X | 模式
Flutter tips: various fancy nesting of listview and pageview
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
Four common methods of copying object attributes (summarize the highest efficiency)
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
ArrayBuffer
xxl-job惊艳的设计,怎能叫人不爱
How to display √ 2 on the command line terminal ̅? This is actually a blog's Unicode test article
About the for range traversal operation in channel in golang
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Leetcode (Sword finger offer) - 35 Replication of complex linked list
Trees and graphs (traversal)
2022-2028 global probiotics industry research and trend analysis report
Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
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
How should PMP learning ideas be realized?
Log cannot be recorded after log4net is deployed to the server
GoLand environment variable configuration