1. Create a Go Modules Project
1
2
| mkdir go-test
cd go-test
|
1
2
3
4
5
| go mod init gitlab.private.com/shaowenchen/go-test
go: creating new go.mod: module gitlab.private.com/shaowenchen/go-test
go: to add module requirements and sums:
go mod tidy
|
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world.",
})
})
r.Run()
}
|
- Download dependencies into vendor
1
2
| go mod tidy
go mod vendor
|
1
2
3
4
5
| go run main.go
[GIN-debug] GET / --> main.main.func1 (3 handlers)
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
|
- Push to the code repository
1
2
3
4
5
| git init
git remote add origin git@gitlab.private.com:shaowenchen/go-test.git
git add .
git commit -m "Initial commit"
git push -u origin master
|
2. How to Pull Private Dependency Packages
If the private repository uses SSH authentication, you need to replace the http/https form with the git@ form.
1
| git config --global url."git@gitlab.private.com:".insteadof "https://gitlab.private.com/"
|
Or edit ~/.gitconfig and add the following:
[url "git@gitlab.private.com:"]
insteadof = https://gitlab.private.com/
- Set environment variables to exempt the private repository
Go Modules uses a proxy to update dependencies by default, so private repository dependency packages need to be exempted. At the same time, there is no GOSUMDB service to verify private dependency packages, so that also needs to be exempted.
1
2
3
| go env -w GOPRIVATE="gitlab.private.com"
go env -w GONOPROXY="gitlab.private.com"
go env -w GONOSUMDB="gitlab.private.com"
|
If the code repository server does not use a valid certificate, you also need to configure the following environment variable:
1
| go env -w GOINSECURE="gitlab.private.com"
|
- Add a new dependency package
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| package main
import (
"github.com/gin-gonic/gin"
"gitlab.private.com/share/log"
)
func main() {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "hello world.",
})
})
log.Info("hello world")
r.Run()
}
|
- Download the dependency package
1
2
3
| go get "gitlab.private.com/share/log"
go mod tidy
go mod vendor
|
1
2
| go run main.go
go build
|
3. Replacing Packages That Cannot Be Downloaded
There are usually two cases where you use replace to substitute a package:
- It cannot be pulled directly, and a mirrored package from another source is needed
- The dependency is still under development and has not been released yet
1
2
3
4
5
6
7
8
| go 1.16
require (
github.com/gin-gonic/gin v1.7.2
gitlab.private.com/share/log v0.0.4
)
replace gitlab.private.com/share/log v0.0.4 => /module/path/log
|
Or
go mod edit -replace=gitlab.private.com/share/log@v0.0.4=/module/path/log
4. How to Customize a Package’s Domain Address
The usual format for adding a dependency package is github.com/gin-gonic/gin, where github.com is the code server address, gin-gonic is the organization name, and gin is the project name.
But the package in Kubernetes is not github.com/kubernetes/kubernetes, it is k8s.io/kubernetes — so how is that implemented?
This requires redirecting k8s.io. Taking the replacement of github.com/gianarb/go-irc with go.gianarb.it/irc using GitHub Pages as an example:
- Create a project named go-libraries
- Add a file with the same name as the project, irc, with the following content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| <html>
<head>
<meta
name="go-import"
content="go.gianarb.it/irc git https://github.com/gianarb/go-irc"
/>
<meta
http-equiv="refresh"
content="0;URL='https://github.com/gianarb/go-irc'"
/>
</head>
<body>
Redirecting you to the
<a href="https://github.com/gianarb/go-irc">project page</a>...
</body>
</html>
|
5. Common Problems
- SUM verification error during
go mody tidy
1
2
3
| verifying gitlab.private.com/mygroup/proto@v0.0.1: checksum mismatch
downloaded: h1:zpwTvQGgQFudfxFgnj9w90do3ps+BQ9ir/Sa7oVPooA=
go.sum: h1:+XAvplGdXmvEank7sOI+Cd3GYdq3dBEDpW4/DO3sSUw=
|
Solution:
1
2
3
| go clean -modcache
rm go.sum
go mod tidy
|
6. References