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]
to initialize the module. Initialized directory:
At this time go.mod is still empty. We know that go build will update go.mod, so we go build first
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 )
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 >, >=, go get -u can update the package to the latest versiongo get -u=patch will only update the minor version , for example, from v1.2.4 to v1.2.5When you want to modify the package version, you only need to go get package@specified versionThen we want to use chromedp instead The latest version is very simple:go get github.com/chromedp/chromedp@masterNow 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 )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/gormUpdated 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 )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.9Unfortunately, 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:
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!

Golangisidealforperformance-criticalapplicationsandconcurrentprogramming,whilePythonexcelsindatascience,rapidprototyping,andversatility.1)Forhigh-performanceneeds,chooseGolangduetoitsefficiencyandconcurrencyfeatures.2)Fordata-drivenprojects,Pythonisp

Golang achieves efficient concurrency through goroutine and channel: 1.goroutine is a lightweight thread, started with the go keyword; 2.channel is used for secure communication between goroutines to avoid race conditions; 3. The usage example shows basic and advanced usage; 4. Common errors include deadlocks and data competition, which can be detected by gorun-race; 5. Performance optimization suggests reducing the use of channel, reasonably setting the number of goroutines, and using sync.Pool to manage memory.

Golang is more suitable for system programming and high concurrency applications, while Python is more suitable for data science and rapid development. 1) Golang is developed by Google, statically typing, emphasizing simplicity and efficiency, and is suitable for high concurrency scenarios. 2) Python is created by Guidovan Rossum, dynamically typed, concise syntax, wide application, suitable for beginners and data processing.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Go language has unique advantages in concurrent programming, performance, learning curve, etc.: 1. Concurrent programming is realized through goroutine and channel, which is lightweight and efficient. 2. The compilation speed is fast and the operation performance is close to that of C language. 3. The grammar is concise, the learning curve is smooth, and the ecosystem is rich.

The main differences between Golang and Python are concurrency models, type systems, performance and execution speed. 1. Golang uses the CSP model, which is suitable for high concurrent tasks; Python relies on multi-threading and GIL, which is suitable for I/O-intensive tasks. 2. Golang is a static type, and Python is a dynamic type. 3. Golang compiled language execution speed is fast, and Python interpreted language development is fast.

Golang is usually slower than C, but Golang has more advantages in concurrent programming and development efficiency: 1) Golang's garbage collection and concurrency model makes it perform well in high concurrency scenarios; 2) C obtains higher performance through manual memory management and hardware optimization, but has higher development complexity.

Golang is widely used in cloud computing and DevOps, and its advantages lie in simplicity, efficiency and concurrent programming capabilities. 1) In cloud computing, Golang efficiently handles concurrent requests through goroutine and channel mechanisms. 2) In DevOps, Golang's fast compilation and cross-platform features make it the first choice for automation tools.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.