Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use GVM to manage Go projects

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use GVM to manage Go projects". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Gvm is a command line tool under Linux, macOS and Windows, which can provide a convenient multi-version go environment for management and switching.

Install GVM

Installing GVM is easy. The GVM repository installation documentation instructs you to download the installer script and transfer it to Bash to install:

Bash

Although more and more people are adopting this installation method, it is still a good idea to see what the installer is doing before installing. Take GVM as an example, the installer script: 1. Check some related dependencies 2. Clone GVM repository 3. Use shell script: (1) install Go language (2) manage GOPATH environment variables (3) add a line to bashrc, zshrc or configuration file if you want to confirm what it is doing, you can clone the repository and view the shell script, and then run the local script. / binscripts/gvm-installer to set up.

Note: because GVM can be used to download and compile new versions of Go, there are expected dependencies such as Make, Git, and Curl. You can find a complete list of distributions in the README file of GVM.

Install and manage GO versions of GVM using GVM

Once GVM is installed, you can use it to install and manage different versions of Go. The gvm listall command displays the available versions of Go that can be downloaded and compiled:

[chris@marvin] $gvm listallgvm gos (available) go1 go1.0.1 go1.0.2 go1.0.3

Installing a specific version of Go is as simple as gvm install, which is one of the versions returned by the gvm listall command. Suppose you are working on a project that uses the Go1.12.8 version. You can install this version using gvm install go1.12.8:

[chris@marvin] $gvm install go1.12.8Installing go1.12.8...* Compiling...go1.12.8 successfully installed!

Type gvm list and you will see that Go version 1.12.8 coexists with the system Go version (packaged using the operating system's package manager):

[chris@marvin] $gvm listgvm gos (installed) go1.12.8= > system

GVM is still using the system version of Go, represented by the = > symbol. You can use the gvm use command to switch your environment to use the newly installed go1.12.8:

[chris@marvin] $gvm use go1.12.8Now using version go1.12.8 [chris@marvin] $go versiongo version go1.12.8 linux/amd64

GVM makes it extremely easy to manage installed versions of Go, but it's more than that!

Use GVM pkgset

Right out of the box, Go has an excellent and frustrating way of managing packages and modules. By default, if you go get gets a package, it will be downloaded to the src and pkg directories in the $GOPATH directory, and then you can include it in your Go program using import. This makes it easy to obtain packages, especially for non-privileged users, without requiring sudo or root privileges (much like pip install-user in Python). However, it is very difficult to manage different versions of the same package in different projects.

There are many ways to try to fix or mitigate this problem, including experimental Go Modules (initial support added in Go version 1.11) and Go dep (Go Modules's "official experiment" and continuous iteration). Before I find GVM, I build and test it in a Go project's own Docker container to ensure separation.

GVM does a good job of managing and isolating packages between projects by using "pkgsets" to attach the new directory of the project to the default of the installed Go version, just as PATH works on Unix/Linux systems.

Imagine how it works. First, install the new version of Go 1.12.9:

[chris@marvin] $echo $GOPATH/home/chris/.gvm/pkgsets/go1.12.8/global [chris@marvin] $gvm install go1.12.9Installing go1.12.9...* Compiling...go1.12.9 successfully installed [chris@marvin] $gvm use go1.12.9Now using version go1.12.9

When GVM is told to use the new version, it changes to the new $GOPATH, and the default gloabl pkgset applies to that version:

[chris@marvin] $echo $GOPATH/home/chris/.gvm/pkgsets/go1.12.9/global [chris@marvin] $gvm pkgset listgvm go package sets (go1.12.9) = > global

Although no additional packages are installed by default, the packages in the global pkgset are available to any project that uses that particular version of Go.

Now, suppose you are enabling a new project that requires a specific package. First, use GVM to create a new pkgset called introToGvm:

[chris@marvin] $gvm pkgset create introToGvm [chris@marvin] $gvm pkgset use introToGvmNow using version go1.12.9@introToGvm [chris@marvin] $gvm pkgset listgvm go package sets (go1.12.9) global= > introToGvm

As mentioned above, a new directory for pkgset is added to $GOPATH:

[chris@marvin] $echo $GOPATH/home/chris/.gvm/pkgsets/go1.12.9/introToGvm:/home/chris/.gvm/pkgsets/go1.12.9/global

Change the directory to the preset introToGvm path and check the directory structure, which is done here using awk and bash.

[chris@marvin] $cd $(awk-favored virtual'{print $1}'$GOPATH) [chris@marvin] $pwd/home/chris/.gvm/pkgsets/go1.12.9/introToGvm [chris@marvin] $lsoverlay pkg src

Notice that the new directory looks a lot like normal $GOPATH. The new Go package is downloaded and used using the same go get command, and added to pkgset. For example, use the following command to get the gorilla/mux package, and then check the directory structure of pkgset:

[chris@marvin] $go get github.com/gorilla/mux [chris@marvin] $tree [chris@marvin introToGvm] $tree. ├── overlay │ ├── bin │ └── lib │ └── pkgconfig ├── pkg │ └── linux_amd64 │ └── github.com │ └── gorilla │ └── mux.asrc/ └── github.com └── gorilla └── mux ├── AUTHORS ├── bench_test.go ├── context.go ├── context_test.go ├── doc.go ├── example_authentication_middleware_test.go ├── example_cors_method_middleware_test.go ├── example_route_test.go ├── go.mod ├── LICENSE ├── middleware.go ├── middleware_test.go ├── mux.go ├── mux_test.go ├── old_test.go ├── README.md ├── regexp.go ├── route.go └── test_helpers.go

As you can see, gorilla/mux has been added to the pkgset $GOPATH directory as expected and is now available for use with this pkgset project.

GVM makes Go management a breeze

GVM is an intuitive and non-invasive way to manage Go versions and packages. It can be used alone or in conjunction with other Go module management technologies and take advantage of GVM Go version management capabilities. Separating projects by Go version and package dependencies makes it easier to develop and reduces the complexity of managing version conflicts, which GVM makes easy.

This is the end of "how to use GVM to manage Go projects". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report