Go module: how to solve the reference problem of private non compliant Library


Go module: how to solve the reference problem of private non compliant Library

Hello, I’m polarisxu.

A friend asked a question:

Go module: how to solve the reference problem of private non compliant Library

Netizen consultation

In actual projects, when using go module, it is inevitable to have some own libraries to reference. These libraries are managed by self built git services, such as gitlab. In view of this situation, many tutorials let you set goprivate, that is, do not go to goproxy.

However, sometimes, not only is the library private, but the address is likely not “compliant”. What is “non-compliance”?

  • Not HTTPS
  • Non 443 or 80 ports
  • Use IP address directly

It’s not HTTPS. It’s mainly non 443 or 80, and the problem of direct IP address is relatively large. Because go does not support the following syntax:

  1. import “studygolang.com:8081/polarisxu/util” 
  2. import “” 

Generally, we should avoid such a situation. After all, it’s disgusting and there’s no need to ask for trouble.

But what if it happens? This paper discusses this problem.

01 simulation environment

Because I haven’t encountered such a problem before, in order to better solve it, this friend directly provided his environment for me to experiment. I registered my account and created a warehouse testgo: http://vitogo.tpddns.cn:9000/polarisxu/testgo 。 (for convenience, this warehouse is public)

Tip: if you want to experiment, you can register an account yourself. Of course, you can also build one locally through gitlab.

Create a file testgo.go in the warehouse, as follows:

  1. package testgo 
  3. func MyName() string { 
  4.  return “polarisxu” 

The key is how to write the go. Mod of this library?

Obviously, we can’t directly use module names like vitogo.tpddns.cn: 9000 / polarisxu / testgo.

02 module name

What can we do?

With the help of GIT, we can replace vitogo.tpddns.cn: 9000: (you can also directly modify the ~ /. Gitconfig file)

  1. git config –global url.”http://vitogo.tpddns.cn:9000/”.insteadof “https://{{gitlab_url}}/” 

Here are two points to note:

1) The content behind the URL and the specific value need to be determined according to your situation.

You can view it through your self built warehouse:

Go module: how to solve the reference problem of private non compliant Library

View clone

HTTP is selected here (because I have created a public repository). You can also choose to use SSH mode, so that you can pull normally even if you configure your own SSH key in a private warehouse. (you can easily find the tutorial on how to configure SSH key on the Internet, which is available on GitHub)

We use it here http://vitogo.tpddns.cn:9000/ , indicating all contents under this domain name.

2) The content after insteadof indicates that when accessing this link, it will be replaced by the link after the above URL.

What does this value say? Obviously, it must be a compliant domain name. Let’s try using any domain name, such as https://studygolang.com/ 。

At this time, we try to execute the following command:

  1. $ git config –global url.”http://vitogo.tpddns.cn:9000/”.insteadof “https://studygolang.com/” 
  2. #   Give Way   studygolang.com   Don’t go   GOPROXY
  3. $ go env -w GOPRIVATE=studygolang.com 
  4. $ go get -v studygolang.com/polarisxu/testgo 
  5. go get: unrecognized import path “studygolang.com/polarisxu/testgo”: parsing studygolang.com/polarisxu/testgo: XML syntax error on line 15: unescaped < inside quoted string 

It’s easy to understand. Go get finally needs to download the code. How can I download it? This picture illustrates:

Go module: how to solve the reference problem of private non compliant Library

Go get process

How do you know that the current warehouse is hosted by VCs? For the domain name studygolang.com, it will try to request and determine the CVS type. Obviously, studygolang.com does not do any processing. It is not a CVS type, so an error is reported.

For those interested in this process, please refer to this article: https://studygolang.com/articles/35235

Netizens want to use vitogo.tpddns.cn, his domain name, but they also have this problem. If you want to make it normal, you need to do special treatment. Refer to the above article for details.

Therefore, we use a very useful off the shelf git public hosting service, such as gitea.com. (polarisxu / testgo I typed a tag: v0.0.1)

  1. $ git config –global url.”http://vitogo.tpddns.cn:9000/”.insteadof “https://gitea.com/” 
  2. $ go env -w GOPRIVATE=gitea.com 
  3. $ go get -v gitea.com/polarisxu/testgo 
  4. get “gitea.com/polarisxu/testgo”: found meta tag vcs.metaImport{Prefix:”gitea.com/polarisxu/testgo”, VCS:”git”, RepoRoot:”https://gitea.com/polarisxu/testgo.git”} at //gitea.com/polarisxu/testgo?go-get=1 
  5. go: downloading gitea.com/polarisxu/testgo v0.0.1 
  6. gitea.com/polarisxu/testgo 

succeed! You can go to $gopath / PKG / mod to see if there is a corresponding package.

Pay attention to it? The parameter go get = 1 can be accessed by the browser https://gitea.com/polarisxu/testgo?go -Get = 1, and then check the source code to see what is inside:

  1. <!doctype html> 
  2. <html> 
  3.  <head> 
  4.   <meta name=”go-import” content=”gitea.com/polarisxu/testgo git https://gitea.com/polarisxu/testgo.git”> 
  5.   <meta name=”go-source” content=”gitea.com/polarisxu/testgo _ https://gitea.com/polarisxu/testgo/src/branch/master{/dir} https://gitea.com/polarisxu/testgo/src/branch/master{/dir}/{file}#L{line}”> 
  6.  </head> 
  7.  <body> 
  8.   go get gitea.com/polarisxu/testgo 
  9.  </body> 
  10. </html> 

Therefore, we can add the go.mod file in polarisxu / testgo:

  1. go mod init gitea.com/polarisxu/testgo 

Then type the second tag: v0.0.2 and get the following again:

  1. $ go get -v gitea.com/polarisxu/testgo 
  2. get “gitea.com/polarisxu/testgo”: found meta tag vcs.metaImport{Prefix:”gitea.com/polarisxu/testgo”, VCS:”git”, RepoRoot:”https://gitea.com/polarisxu/testgo.git”} at //gitea.com/polarisxu/testgo?go-get=1 
  3. go: downloading gitea.com/polarisxu/testgo v0.0.2 
  4. gitea.com/polarisxu/testgo 

03 use this package

Create a project locally and reference the package defined above:

  1. $ mkdir ~/testprivate 
  2. $ cd ~/testprivate 
  3. $ go mod init testprivate 
  4. $ touch main.go 

Enter the following in main.go:

  1. package main 
  3. import ( 
  4.     “fmt” 
  5.     “gitea.com/polarisxu/testgo” 
  7. func main() { 
  8.     fmt.Println(“Hello”, testgo.MyName()) 

After executing go mod tidy, run:

  1. $ go run main.go 
  2. Hello polarisxu 

Normally output the results we expect.

04 summary

By understanding the basic principle of go get, know some processing methods of GIT and the role of goprivate. I believe you can solve similar problems yourself.

Note that if you actually use gitea.com, you can choose gitee.com, try.gogs.io, etc.

In addition, go has several issues related to this article:



Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]