当前位置:网站首页>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/
边栏推荐
- 《网络是怎么样连接的》读书笔记 - WEB服务端请求和响应(四)
- 【leetcode】540. A single element in an ordered array
- 2022-2028 global small batch batch batch furnace industry research and trend analysis report
- Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
- Daughter love in lunch box
- 2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
- Kubernetes CNI 插件之Fabric
- Write a jison parser from scratch (6/10): parse, not define syntax
- At the age of 30, I changed to Hongmeng with a high salary because I did these three things
- Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
猜你喜欢

2022-2028 global edible probiotic raw material industry research and trend analysis report

2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors

Summary of small program performance optimization practice

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

mmclassification 标注文件生成

C # use ffmpeg for audio transcoding

Some points needing attention in PMP learning

JDBC and MySQL database

QTreeView+自定义Model实现示例

How does idea withdraw code from remote push
随机推荐
pcl::fromROSMsg报警告Failed to find match for field ‘intensity‘.
Pueue data migration from '0.4.0' to '0.5.0' versions
Flutter tips: various fancy nesting of listview and pageview
Luogu deep foundation part 1 Introduction to language Chapter 4 loop structure programming (2022.02.14)
2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report
技术管理进阶——如何设计并跟进不同层级同学的绩效
Are there any principal guaranteed financial products in 2022?
The child container margin top acts on the parent container
自动化的优点有哪些?
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
Upgrading Xcode 12 caused Carthage to build cartfile containing only rxswift to fail
Trees and graphs (traversal)
el-table单选并隐藏全选框
Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
How does idea withdraw code from remote push
Multilingual Wikipedia website source code development part II
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
《网络是怎么样连接的》读书笔记 - 认识网络基础概念(一)
C # use ffmpeg for audio transcoding
PHP student achievement management system, the database uses mysql, including source code and database SQL files, with the login management function of students and teachers