Home >Common Problem >Are there any dependent packages in Go language?
There are dependent packages in the Go language. The methods for installing dependent packages are: 1. Use the "go get" command to install the dependent packages; 2. Turn on "go mod", and then use "" in the project directory go get" to pull the package; 3. Manually download the dependent package in github and put it in the corresponding directory; 4. Copy the corresponding package under "GOPATH/pkg/mod"; 5. Put the code directly into the project, and then use " go tidy" can automatically organize package dependencies.
Operating system for this tutorial: Windows 10 system, go1.20 version, Dell G3 computer.
There are dependent packages in the Go language.
Although go now has a relatively easy-to-use go mod package management tool. However, due to some indescribable reasons, when installing dependencies There are still many problems for novices, let’s take a look at how to solve them?
1. Go get
from Chapter 1 One day when you come into contact with the package dependencies of go, your teacher or the information you read will tell you: directly go get
.
This is the most original installation method, which can solve most of the problems. Package dependency issues.
But not 100% of them can be installed successfully. For example, the package in the screenshot above: golang.org/x/crypto/ssh
.
Directly go get
, the golang.org URL may not be accessible due to indescribable reasons, and the package cannot be installed successfully.
2. Configure GOPROXY
go mod
is a newly added feature of go 1.11.
So as long as the go version is greater than 1.11, it comes with go mod
package management.
This is a good thing, if go mod
is enabled.
go mod is initialized in the project,
then use go in the project directory get
will automatically use go mod
to pull the package and organize it into the go.mod file.
But the source used by default is from abroad, so installing dependent packages is generally Very slow, or unable to succeed.
So you need to set up their proxy to let them go out from the domestic server, so that the speed and stability will be better.
If you are using goland You can command,
to call up the preferences and set the proxy in it:
Recommended proxy to https://goproxy.cn, Qiniu Cloud Home, I have been using it, it is very stable.
After modification, remember to reopen the terminal!
If you are using other IDE or command line, how to set it up on Baidu yourself? acting.
After the setting is completed, use go env
to see the environment variables currently used by go, which can be used to check whether the configuration is OK.
3. Use github
If the installation cannot be successful after the above two steps, you have to use abnormal means to install dependencies.
Package reference
First you need to understand how go local packages are stored and referenced.
GOPATH/src/domain name/package name
. GOPATH/pkg/mod/domain name/package name@version number
. So you can manually download the package and put it in the corresponding directory.
Manually download the package
It is not completely inaccessible to GitHub in China, but it may be slower or often unable to be opened. You can try your luck at this time.
Official packages can be found under this warehouse: https://github.com/golang
How to There will be a description in the README.md of each package installed:
The easiest way to install is to run go get -u http://golang.org/x/net. You can also manually git clone the repository to $GOPATH/src/http://golang.org/x/net.
Most of the dependent packages can be found in github.
4. Use connections
Follow the idea of the previous solution. Not everyone cannot surf the Internet scientifically. You can seek friends around you who can surf the Internet scientifically to help you download. Package for you.
Then copy the corresponding package below GOPATH/pkg/mod
.
5. Use go mod
Finally there is another scenario, which is to know the package name and get some sample demos through the documentation.
Put the code directly into the project, and then use go tidy
to automatically regulate package dependencies.
For example: github.com/gin-gonic/gin
Most registrations are the warehouse address where the code is located, and most of them can be accessed directly.
There is a very interesting thing about dependent packages. Sometimes the packages used are packages inside packages.
It may be a bit convoluted, but it’s easy to understand.
For example, at the beginning of the article: golang.org/x/crypto/ssh
This package,
is golang.org/x/crypto
package below.
So if you install the ssh package directly, the package will often not be found, so you need to install it at the upper level.
The above is the detailed content of Are there any dependent packages in Go language?. For more information, please follow other related articles on the PHP Chinese website!