Home  >  Article  >  Are there any dependent packages in Go language?

Are there any dependent packages in Go language?

小老鼠
小老鼠Original
2023-04-17 16:14:55999browse

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.

Are there any dependent packages in Go language?

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?

Are there any dependent packages in Go language?

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:

Are there any dependent packages in Go language?

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.

  • If it is a traditional go get downloaded dependency package, it will be stored under GOPATH/src/domain name/package name.
  • If you use go mod, the downloaded dependency package will be stored under 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: 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.

Some common sense

  • 1. The package name is the warehouse address

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.

  • 2. Packages inside packages

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/sshThis 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!

Statement:
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