Home Backend Development Golang Introduction to using go get for package management in go modules

Introduction to using go get for package management in go modules

Nov 30, 2019 pm 02:18 PM
go

Introduction to using go get for package management in go modules

Package management under module

First we introduced go mod edit to modify go.mod, but it has Two flaws:

1. First, its -require must accept the form of "package@version", which is indispensable, and it cannot recognize the master and latest flags specified in the document.

2. Secondly, edit is only suitable for modifying dependency versions, renaming packages, and blocking specific packages. It is not suitable for adding dependencies.

The good news is that go get now has the ability to add/modify/update packages in modules.

To fully experience go modules, we need to select a directory other than GOPATH and set GO11MODULE=on. In this way, using go get will only affect the current main module and will not pollute GOPATH.

We clone the project to a non-GOPATH path, and then use

go mod init [project name]
Copy after login

to initialize the module. Initialized directory:

Introduction to using go get for package management in go modules

At this time go.mod is still empty. We know that go build will update go.mod, so we go build first

Introduction to using go get for package management in go modules

By default, go get will be used to obtain the latest package. Now go.mod has been updated and the project has been successfully compiled. This is the meaning of go.mod:

module schanclient

require (
    github.com/PuerkitoBio/goquery v1.4.1
    github.com/andybalholm/cascadia v1.0.0 // indirect
    github.com/chromedp/chromedp v0.1.2
    golang.org/x/net v0.0.0-20180826012351-8a410e7b638d // indirect
)
Copy after login

indirect It means that this package is dependent on the sub-module/package, but the main module is not directly imported and used, which is the so-called indirect reference.

Usually, go.mod can complete package management well using the default behavior, but there are always some exceptions in life.

We see that chromedp uses version 0.1.2, which is the version three months ago. The latest commit was last month. Go mod edit needs to clearly specify the version number or commit time checksum. Obviously This is cumbersome and not what we want.

So how can we use the latest version instead of the latest tags?

Or maybe we don’t want the latest version and need a specific version of the package?

This is the content of version selection.

New features of go get - version selection

There used to be a solution like gopkg.in go get, but the new go The version selection supported by get is a further expansion of this solution. Look at a few rules:

go get will automatically download and install the package, and then update it to go.mod

can be used go get package[@version] to install the specified version of the package. When version is not specified, the default behavior is the same as go get package@latest

version can be in the form of vx.y.z or directly use the commit checksum, or It can be master or latest

When version is latest, it is the default behavior. For packages with tags, the latest tag will be selected. For packages without tags, the latest commit

## will be selected. #When version is master, regardless of whether the package is tagged, the latest commit of the master branch will be selected.

You can use >, >=, <, <= before version to indicate the selected version. It must not exceed/below the version. The versions within this range that meet the latest conditions

go get -u can update the package to the latest version

go get -u=patch will only update the minor version , for example, from v1.2.4 to v1.2.5

When you want to modify the package version, you only need to go get package@specified version

Then we want to use chromedp instead The latest version is very simple:

go get github.com/chromedp/chromedp@master
Copy after login

Now chromedp has been updated in go.mod:

module schanclient

require (
	github.com/PuerkitoBio/goquery v1.4.1
	github.com/andybalholm/cascadia v1.0.0 // indirect
	github.com/chromedp/chromedp v0.1.3-0.20180717231922-bf52fed0d3e6
	golang.org/x/net v0.0.0-20180826012351-8a410e7b638d // indirect
)
Copy after login

What if we want to add additional packages now?

Just use go get directly. For example, I now want to use gorm to store data:

go get github.com/jinzhu/gorm
Copy after login

Updated go.mod

module schanclient

require (
	github.com/PuerkitoBio/goquery v1.4.1
	github.com/andybalholm/cascadia v1.0.0 // indirect
	github.com/chromedp/chromedp v0.1.3-0.20180717231922-bf52fed0d3e6
	github.com/jinzhu/gorm v1.9.1 // indirect
	github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a // indirect
	golang.org/x/net v0.0.0-20180826012351-8a410e7b638d // indirect
)
Copy after login

We see the latest version The gorm has been added, of course because we did not import it in the main module, so it is indirect.

If we want to use v1.9 gorm:

go get github.com/jinzhu/gorm@v1.9
Copy after login
Unfortunately, the version selection is from the major version to the minor version. If there are v1.9 and v1.9.1, then when When you specify v1.9, the version with the highest minor version number will be automatically selected, unless there is no other v1.9.z tag except v1.9, which is v1.9.1 in this case.

Another point worth mentioning is that go build and go test will only add packages that are not in go.mod and will not overwrite or change the rules introduced by go get, so there is no need to worry about them conflicting.

Do you think it is similar to venv pip? Yes, this shows that go's package management tools are gradually becoming modern.

As for blocking packages, deleting packages and renaming packages (such as inaccessible packages at golang.org/x/...), these are the functions of go mod edit. For details, please see go help mod edit .

Recommended:

go language tutorial

The above is the detailed content of Introduction to using go get for package management in go modules. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to send Go WebSocket messages? How to send Go WebSocket messages? Jun 03, 2024 pm 04:53 PM

In Go, WebSocket messages can be sent using the gorilla/websocket package. Specific steps: Establish a WebSocket connection. Send a text message: Call WriteMessage(websocket.TextMessage,[]byte("Message")). Send a binary message: call WriteMessage(websocket.BinaryMessage,[]byte{1,2,3}).

In-depth understanding of Golang function life cycle and variable scope In-depth understanding of Golang function life cycle and variable scope Apr 19, 2024 am 11:42 AM

In Go, the function life cycle includes definition, loading, linking, initialization, calling and returning; variable scope is divided into function level and block level. Variables within a function are visible internally, while variables within a block are only visible within the block.

The difference between Golang and Go language The difference between Golang and Go language May 31, 2024 pm 08:10 PM

Go and the Go language are different entities with different characteristics. Go (also known as Golang) is known for its concurrency, fast compilation speed, memory management, and cross-platform advantages. Disadvantages of the Go language include a less rich ecosystem than other languages, a stricter syntax, and a lack of dynamic typing.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

How to avoid memory leaks in Golang technical performance optimization? How to avoid memory leaks in Golang technical performance optimization? Jun 04, 2024 pm 12:27 PM

Memory leaks can cause Go program memory to continuously increase by: closing resources that are no longer in use, such as files, network connections, and database connections. Use weak references to prevent memory leaks and target objects for garbage collection when they are no longer strongly referenced. Using go coroutine, the coroutine stack memory will be automatically released when exiting to avoid memory leaks.

How to view Golang function documentation in the IDE? How to view Golang function documentation in the IDE? Apr 18, 2024 pm 03:06 PM

View Go function documentation using the IDE: Hover the cursor over the function name. Press the hotkey (GoLand: Ctrl+Q; VSCode: After installing GoExtensionPack, F1 and select "Go:ShowDocumentation").

A guide to unit testing Go concurrent functions A guide to unit testing Go concurrent functions May 03, 2024 am 10:54 AM

Unit testing concurrent functions is critical as this helps ensure their correct behavior in a concurrent environment. Fundamental principles such as mutual exclusion, synchronization, and isolation must be considered when testing concurrent functions. Concurrent functions can be unit tested by simulating, testing race conditions, and verifying results.

Things to note when Golang functions receive map parameters Things to note when Golang functions receive map parameters Jun 04, 2024 am 10:31 AM

When passing a map to a function in Go, a copy will be created by default, and modifications to the copy will not affect the original map. If you need to modify the original map, you can pass it through a pointer. Empty maps need to be handled with care, because they are technically nil pointers, and passing an empty map to a function that expects a non-empty map will cause an error.

See all articles