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

What are the problems encountered in using go mod

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the problems encountered in using go mod". 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!

Question 1: where are the dependent packages downloaded? Is it still in GOPATH?

He's not here.

Using Go package management, dependent third-party packages are downloaded to the $GOPATH/pkg/mod path.

Question 2: how is the version of the dependency package controlled?

In the previous question, you can see that the final package downloaded under $GOPATH/pkg/mod will end up with a version number v1.0.5, that is, different versions of the same package can be saved in $GOPATH/pkg/mod.

The version is specified in go.mod. If not specified in go.mod, the go command automatically downloads the latest version of the dependency in the code, in this case the latest version. If you specify the package and version in go.mod with the upload statement, the go command downloads the package according to the specified path and version

You can use latest when specifying a version, so it automatically downloads the latest version of the specified package

Question 3: can I put the project under $GOPATH/src?

Sure. But go will handle it differently depending on the value of GO111MODULE, and by default, GO111MODULE=auto automatic mode

In auto automatic mode, the project uses the dependent package of $GOPATH/src in $GOPATH/src, and the package of require in go.mod outside of $GOPATH/src

On enables mode. After 1.12, both inside and outside the $GOPATH/src, the require package in go.mod will be used.

Off off mode is the old rule.

Question 3: what if the address in the dependency package fails? Such as golang.org/x/... What if none of the packages can be downloaded?

In the course of the rapid development of go, some dependent package addresses have changed. Previous practice:

Modify the source code to replace the address of import with a new path

After the new package of git clone or go get, copy to the old path in $GOPATH/src

No matter what method, it is not easy to maintain, especially when multi-person collaborative development.

It's easy to use go.mod, replace the package with replace in the go.mod file, for example

Replace golang.org/x/text = > github.com/golang/text latest

In this way, go will replace golang.org/x/text with github.com/golang/text by downloading the latest version of github.com/golang/text to $GOPATH/pkg/mod/golang.org/x/text.

Question 4: what is the use of the module name of the go.mod generated by init?

In this case, the first line in the go.mod file generated with go mod init hello declares module hello

Since our project is no longer in $GOPATH/src, what about quoting yourself? Just use the module name + path.

For example, create a new directory utils under the project and create a tools.go file:

Package utilsimport "fmt" func PrintText (text string) {fmt.Println (text)}

The hello.go file in the root directory can import "hello/utils" to refer to utils.

Package mainimport ("hello/utils"github.com/astaxie/beego") func main () {utils.PrintText ("Hi") beego.Run ()} question 5: how to manage old projects with new packages

If you use auto mode, move the project outside of $GOPATH/src

Enter the directory and run go mod init + module name

Go build or go run once.

This is the end of the content of "what are the problems encountered in using go mod"? 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

Internet Technology

Wechat

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

12
Report