当前位置:网站首页>Go language starts again, go modules' past life, present life and basic use
Go language starts again, go modules' past life, present life and basic use
2022-06-24 02:21:00 【Bain】
Click on One click subscription 《 Yunjian coffee 》 special column , Get the official recommended boutique content , Learn technology without getting lost !
2020 In, a developer report within Tencent showed that ,Go Language has become The second largest back-end development language in Tencent , In Tencent, there are a lot of Go Developers are doing business and platform development , The use of a large number of teams and projects also exposed some problems , With Go Modules Appearance , Similar to internal self issued certificates 、 These problems such as safety audit have been gradually solved .
The author is currently in charge of Tencent cloud Go Some problems in the use of programming language , 2021 Responsible for internal management since the beginning of the year goproxy Serve and promote Go Modules Use , These technologies support Tencent cloud 、 WeChat 、 Tencent video 、 Tencent game 、 Tencent music 、 Tencent conference and other star products , And work with the company's internal software source team 、 Worker bee team 、TRPC The team and each CI Close cooperation between the team . In this series of articles , The author will help you begin to learn and understand Go Modules.
Golang Development model evolution
from Go From birth , Users have been using GOPATH This environment variable , With Go The rapid development and growth of language , from GOPATH The compilation dependency problem caused by is also beginning to appear . Finally in the 2019 year , Golang welcome 10 Anniversary ,Google Go The team finally began to focus on this company Golang Ten year environmental variables .
GOPATH Use
Currently in Go There are two development modes in ,GOPATH mode and Go modules mode.
stay Go modules Before ,Go Dependency management in development uses GOPATH Development mode . stay GOPATH In the development mode ,Go Command to use GOPATH Environment variables to achieve the following Several functions :
- go install Command to install binary libraries to $GOBIN, The default path is $GOPATH/bin.
- go install Command to install the compiled package to $GOPATH/pkg/ in , For example example.com/y/z The installation to $GOPATH/pkg/example.com/y/z.a.
- go get Command to download the source package to $GOPATH/src/ in , For example example.com/y/z Download to $GOPATH/src/example.
Go modules Development history of
GOPATH mode The development model will eventually be eliminated ,Go Official throughout Go Add... To the development ecosystem package version The concept of , And then introduce Go modules Development mode . from GOPATH mode Change the development mode to Go modules It's a long process , It has gone through Several Go The distribution version of :
- Go 1.11 (2018 year 8 month ) Introduced GO111MODULE environment variable , The default value is auto. If you set this variable GO111MODULE=off, that go The command will always use GOPATH mode Development mode . If you set this variable GO111MODULE=on,go The command will always use Go modules Development mode . If you set this variable GO111MODULE=auto ( Or not set ),go The command line will decide which mode to use based on the current working directory , If the current directory is $GOPATH/src outside , And exists in the root directory go.mod file , that go The command enables Go module Pattern , Otherwise use GOPATH Development mode . This rule ensures that all in $GOPATH/src Use in auto Value, the original compilation is not affected , And you can also experience the latest... In other directories Go module Development mode .
- Go 1.13 (2019 year 8 month ) Adjust the GO111MODULE=auto In mode pair $GOPATH/src The limitation of , If a code base is in $GOPATH/src in , And there are go.mod Existence of file , go The command enables module Development mode . This allows users to continue to organize their checkout code in an import based hierarchy , However, the module is used to import individual warehouses .
- Go 1.16 (2021 year 2 month ) Will GO111MODULE=on As default , Enabled by default go module Development mode , in other words , By default GOPATH The development mode will be completely shut down . If the user needs to use GOPATH Development patterns can specify environment variables GO111MODULE=auto perhaps GO111MODULE=off.
- Go 1.NN (???) Will be abandoned GO111MODULE Environment variables and GOPATH Development mode , The default is to use completely module Development mode .
GOPATH And Go modules Love wants to kill
For several issues of concern , The author gives the following answers :
Q1:GOPATH Will variables be removed ?
A: Can't ,GOPATH Variable Will not be removed . Future abandonment GOPATH Development mode does not mean deleting GOPATH environment variable , It will remain , The main functions are as follows :
- go install Command to install binary to $GOBIN Catalog , Its default location is $GOPATH/bin.
- go get Command cache downloaded modules To $GOMODCACHE Catalog , The default location is $GOPATH/pkg/mod.
- go get Command cache downloaded checksum Data to $GOPATH/pkg/sumdb Catalog .
Q2: I can continue to `GOPATH/src/import/path` Create a code base in ?
A: Sure , Many developers use this file structure to organize their own repositories , You only need to In your own warehouse Add go.mod file .
Q3: If I want to test and modify a dependency library I need , How can I do it ?
A: If you rely on unpublished changes when compiling your project , You can use go.mod Of replace To fulfill your needs .
for instance , If you have already golang.org/x/website and golang.org/x/tools Download to $GOPATH/src/ Under the table of contents , Then you can go to $GOPATH/src/golang.org/x/website/go.mod Add the following instructions to complete the replacement :
replace golang.org/x/tools => $GOPATH/src/golang.org/x/tools
Of course ,replace Instructions are not perceived GOPATH Of , You can download the code to other directories as well .
from 0 Start using Go Modules
1. Create a new Go module
First create a new directory /home/gopher/hello, Then go to this directory , Then create a new file , hello.go:
package hello
func Hello() string {
return "Hello, world."
}
Then write a corresponding test file hello_test.go:
package hello
import "testing"
func TestHello(t *testing.T) {
want := "Hello, world."
if got := Hello(); got != want {
t.Errorf("Hello() = %q, want %q", got, want)
}
}
Now we have a package, But it's not a module, Because it hasn't been created yet go.mod file . If in /home/gopher/hello Execute... In directory go test, You can see :
$ go test
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
You can see Go Command line prompt not found go.mod file , You can refer to go help modules. In this case, you can use Go mod init Let's initialize , And then execute Go test:
$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello
go: to add module requirements and sums:
go mod tidy
$go mod tidygo
$ go test
PASS
ok example.com/hello 0.020s
$
In this case ,module The test is done .
And then execute go mod init The command creates a go.mod file :
$ cat go.mod
module example.com/hello
go 1.17
2. to module Add dependency
Go modules The main highlight of is to use code written by others when programming , That is, when you introduce a dependency library, you can have a very good experience . First update hello.go, introduce rsc.io/quote To implement some new functions .
package hello
import "rsc.io/quote"
func Hello() string {
return quote.Hello()
}
then , Test it again :
$ go test
hello.go:3:8: no required module provides package rsc.io/quote; to add it:
go get rsc.io/quote
$ go get rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added rsc.io/quote v1.5.2
go: added rsc.io/sampler v1.3.0
$ go test
PASS
ok example.com/hello 1.401s
from Go 1.7 Start ,Go modules Start using lazyloading Loading mechanism , The dependent library needs to be updated manually according to the prompt .
Go The order will be based on go.mod To parse and pull the specified dependent version . If in go.mod The specified version... Was not found in , The corresponding command will be prompted to guide the user to add , then Go The command will parse the latest stable version (Latest), And add to go.mod In file . As you can see in this example , First executed Go test Running requires rsc.io/quote This dependence , But in go.mod Not found in the file , So guide the user to get latest edition , User pass go get rsc.io/quote Got the latest version v1.5.2, And I downloaded two more rsc.io/quote Dependency needed :rsc.io/sampler and golang.org/x/text. Indirect dependency references are also recorded in go.mod In file , Use indirect Mark with comments .
$ cat go.mod
module example.com/hello
go 1.17
require (
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
rsc.io/quote v1.5.2 // indirect
rsc.io/sampler v1.3.0 // indirect
)
Run... Again go test The command will not repeat the above work , because go.mod It's up to date , And the required dependent packages have been downloaded to the machine ( stay $GOPATH/pkg/mod in ):
$ go test
PASS
ok example.com/hello 0.020s
Be careful , although Go Command can quickly and easily add new dependencies , but It's not without cost .
As mentioned above , Adding a direct dependency to a project may introduce other indirect dependencies .go list -m all The command can list all dependencies on which the current project depends :
$ go list -m all
example.com/hello
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0
stay go list In the output of , You can see the current module, Also known as main module Will be shown on the first line , Then the others will follow module path Sort . It depends on golang.org/x/text Version number of v0.0.0-20170915032832-14c0d48ead0c Is a pseudo version number , It is Go A version of , Points to a non hit tag Of commit On .
Besides go.mod file ,go The command also maintains a program called go.sum The file of , This file contains the encrypted hash value corresponding to each version .
$ cat go.sum
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:qgOY6WgZO...
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:Nq...
rsc.io/quote v1.5.2 h1:w5fcysjrx7yqtD/aO+QwRjYZOKnaM9Uh2b40tElTs3...
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPX...
rsc.io/sampler v1.3.0 h1:7uVkIFmeBqHfdjD+gZwtXXI+RODJ2Wc4O7MPEh/Q...
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9...
Go Command to use go.sum To ensure that the dependency library code downloaded each time is consistent with the first time , So as to ensure that there are no exceptions in the project , therefore go.mod and go.sum Should be uploaded to git In the version control system .
3. Update dependency
From above go list -m all In the output of the command , You can see in the library golang.org/x/text A pseudo version number is used in . First, update this version to the latest stable version :
$ go get golang.org/x/text
go: downloading golang.org/x/text v0.3.7
go: upgraded golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c => v0.3.7
$ go test
PASS
ok example.com/hello 0.013s
The test can still pass , Then do it again go list -m all:
$ go list -m all
example.com/hello
golang.org/x/text v0.3.7
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0
$ cat go.mod
module example.com/hello
go 1.17
require (
golang.org/x/text v0.3.7 // indirect
rsc.io/quote v1.5.2 // indirect
rsc.io/sampler v1.3.0 // indirect
)
Conclusion
Go adopt Go modules The dependency management is unified Go Many third parties in the ecology rely on Management , And highly integrated in Go Command line , No need for developers to install and use , Currently under maintenance Go The version already supports Go modules. Haven't switched to Go modules Users of , We strongly recommend that you start using it , Whether it's from the team development experience 、 Performance or safety , Can provide many high-quality features and guarantees .
One click subscription column , Learn technology without getting lost
《 Yunjian coffee 》 It's Tencent cloud plus community boutique content column . Cloud recommendation officer specially invites industry leaders , Focus on the landing of cutting-edge technology and theoretical practice , Continue to interpret the hot technologies in the cloud era for you 、 Explore new opportunities for industry development . One click subscription , We will regularly push premium content for you .
p.s. The cloud sponsor will randomly select some subscription partners , Send out Tencent Industry Conference 、 Tickets for the meeting 、 Cloud plus video gift box 、 Tencent doll !
Big guy resident column , Answer questions for you
Have a question ? Have an insight ? Want to explore ? What works are expected from the teacher ? Welcome to ask questions in the comments section of this article 、 communication , The teacher will answer for you .
The cloud recommendation officer will draw 1 A little friend sent a cloud plus video gift box !
边栏推荐
- The easydss on demand file upload interface calls postman to report an error. Failed to upload the file?
- How about Tencent cloud game server? Can the cloud game server play games
- Embedded hardware development tutorial -- Xilinx vivado HLS case (3)
- Frequent screen flashing after VNC login - abnormal system time
- Shopify has quietly taken the second place in e-commerce in North America. Is independent station the "magic weapon" to win?
- How to confirm whether IPv6 is enabled for a website
- Micro850 Simulator
- Implementing cos signature with postman
- An attempt to use Navicat tool to copy and export MySQL database data
- Advanced BOM tool intelligent packaging function
猜你喜欢

163 mailbox login portal display, enterprise mailbox computer version login portal

application. Yaml configuring multiple running environments

2020 language and intelligent technology competition was launched, and Baidu provided the largest Chinese data set

Introduction to development model + test model

BIM model example

How to fill in and register e-mail, and open mass mailing software for free

Leetcode969: pancake sorting (medium, dynamic programming)

If there are enumerations in the entity object, the conversion of enumerations can be carried out with @jsonvalue and @enumvalue annotations

Stm32g474 infrared receiving based on irtim peripherals

Advanced BOM tool intelligent packaging function
随机推荐
Offline store + online mall, why do you want to be an online mall
Case of data recovery by misoperation under NTFS file system
Tencent Ding Ke: the mission of Tencent security is to safeguard the beauty of digital
What is the reason why the switching page group disappears after easycvr establishes a multi-level group?
Code 128 barcode details
Tencent cloud won the first place in the cloud natural language understanding classification task
The core battlefield of China US AI arms race: trillion level pre training model
Frequent screen flashing after VNC login - abnormal system time
Echo framework: add API logging Middleware
What is ITF barcode
How to formulate a domain name trademark registration scheme? What if the plan is rejected?
[Tencent cloud double 12 audio and video communication special session] from 9 yuan for Q4 counter attack artifact, SMS and security (New) package!
WordPress site quickly integrates Tencent's digital identity management and control platform CIAM to realize login authentication without development
The United States offered 10million yuan to hunt down blackmail hackers and the energy industry became the "hardest hit" of phishing attacks | global network security hotspot
Embedded hardware development tutorial -- Xilinx vivado HLS case (2)
How to build an enterprise website? Is it difficult?
How long can the trademark registration be completed? How to improve the speed of trademark registration?
The cloud University of "digital and real integration, CO building authentic Internet" is surging forward
How to handle the abnormal state of easycvr national standard cascading superior display?
Micro850 Simulator