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 go modules uses local libraries, compliance libraries, and private libraries

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Editor to share with you how go modules uses local libraries, compliance libraries, private libraries, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

I. brief introduction

When a company project uses go module to develop, it will have its own libraries to reference. These libraries are managed by gitlab, and you need to study the methods of referencing gitlab libraries. Here are some experiences from the research process.

Introduce two referencing scenarios:

1. Reference the local directory

2. Reference Compliance address Library

3. Reference private non-compliance libraries

4. Disgusting private non-compliance library

Compliance in the above nouns refers to:

The address of the [x] library is accessed through the domain name

[X] support for HTTPS

[X] the address of the library is through ports 80 and 443, not other ports

Meeting the above conditions is the compliance library, otherwise it is the non-compliance library. Since the non-compliance library is usually private, it is directly called the private non-compliance library. Among them, the violation of point 3 is the most disgusting (the authorities do not support it and have to complain about the situation while raising demand for the authorities).

Note: this article was written on July 3, 2020, go version 1.14.4, until the date of writing, the content is true and valid, all have been verified by myself.

Second, reference the local directory

Modify the file go.mod:

Module maingo 1.14 require / replacement rule replace (github.com/pborman/uuid = > / data/go-packages/uuid v1.2.0 / / either absolute or relative path is OK) / / Source address require (github.com/pborman/uuid v1.2.0)

By default, the github.com/pborman/uuid package is downloaded to Github, but the package is not push online in local development, so you can redirect the loading path of the package to the current project through replace configuration:

Github.com/pborman/uuid = > / data/go-packages/uuid

Here / data/go-packages/uuid is the absolute path of the local package, so you can write it this way. When the local uuid package code is modified, you can see the real-time effect in the current project. Note that the intermediate symbol is = >

After the configuration, the code is pulled, compiled and debugged normally.

II. Citing the compliance library

Similar to referencing the local directory, modify the file go.mod:

Module maingo 1.14 require / replacement Rule replace (github.com/pborman/uuid = > gitee.com/gober/uuid v1.2.0 / / Open Source China address) / / Source address require (github.com/pborman/uuid v1.2.0)

Then pull the code normally, compile, and debug.

III. Citing private non-compliance libraries

Here we introduce the second situation.

3.1. Configure access to the warehouse

There are two ways to authorize:

1. Access token (Access Token)

Access to the repository is achieved by generating an individual "access token" in gitlab and configuring Token on the machine that pulls the git repository.

It is usually at this address: https://{{gitlab_url}}/profile/personal_access_tokens

{{gitlab_url}} is the gitlab address

2. SSH key (SSH Key) [recommended]

The client can access the repository by configuring the SSH key under the gitlab personal account or warehouse.

Note: this method is efficient and simple, as long as there is a corresponding key pair in the local .ssh directory.

3.2. configure the "access token (Access Token)" parameter locally

Ssh key authorization ignores this section

Effective for this project:

Git config http.extraheader "PRIVATE-TOKEN:YOUR_PRIVATE_TOKEN"

Effective for all git projects:

Git config-global http.extraheader "PRIVATE-TOKEN:YOUR_PRIVATE_TOKEN"

Note: you can choose one of the above configurations according to your needs.

3.3.The configuration uses git to pull the substitution code git config-- global url. "git@ {{gitlab_url}}: groupName/projectName.git" .Einstein of "https://{{gitlab_url}}/groupName/projectName.git"`

You can also directly replace globally, so you can pull all the packages under the domain name, such as:

Git config-- global url. "git@ {{gitlab_url}}:" .insteadof "https://{{gitlab_url}}/"

You can also modify the configuration file ~ / .gitconfig directly, like this, and add the following configuration to it:

[url "git@ {{gitlab_url}}:"] insteadOf = https://{{gitlab_url}}/

Note:

The parameter in insteadof is https, because https access is used by default regardless of whether the destination address is http or https,go get, so we force the conversion of https to git protocol.

Fifth, disgusting private non-compliance library

Why is there a disgusting library of private nonconformities? What are the characteristics of this kind of library?

This library tramples all the standards of the compliance library, which means it violates every rule:

1. Ip access

2. Port is not 80,443

3. No certificate

The third point is not important, and the second is the root cause of this situation.

Under such circumstances, we have to:

1. Combine with "3. Quote private non-compliance library".

2. Configure aliases (mapping) in go.mod and git config

Redirect to IP:Port through two aliases.

2.1.Configuring an address alias in go.mod, which must be a real and valid HTTPS address

Go.mod configuration:

Module maingo 1.14 HTTPS / replacement rule replace (github.com/pborman/uuid = > gitee.com/gober/uuid v1.2.0 / / first alias address, address is really valid, and is HTTPS) / / Source address require (github.com/pborman/uuid v1.2.0)

2.2. Configure the alias in git config again to point the alias in go.mod to the real address

Git config-- global url. "git@192.168.102.73:your/git/repos/path" .Einstein of "https://gitee.com/gober""

These are all the contents of the article "how go modules uses local libraries, compliance libraries, and private libraries". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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