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 to 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
|
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, then you need to replace http/https
with the form git@
.
1
|
git config --global url."git@gitlab.private.com:".insteadof "https://gitlab.private.com/"
|
Or modify ~/.gitconfig
to add the following.
1
2
|
[url "git@gitlab.private.com:"]
insteadof = https://gitlab.private.com/
|
- Set environment variables to exempt private repositories
Go Modules uses proxies to update dependencies by default, and requires exemptions for private repository dependencies. Also, there is no GOSUMDB service to verify private dependencies, so it needs to be exempted as well.
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 legal certificate, you also need to configure the following environment variables.
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. replace a package that cannot be downloaded
There are two cases where you can use replace to replace a package:
- a package that cannot be pulled directly and needs to be mirrored from another source
- dependent package, under development, not yet released
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
1
|
go mod edit -replace=gitlab.private.com/share/log@v0.0.4=/module/path/log
|
4. How to customize the package domain address
The usual format for adding dependency packages 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.
However, the package in Kuberntes is not github.com/kubernetes/kubernetes
, but k8s.io/kubernetes
, so how is this achieved?
Here you need to redirect k8s.io, replacing github.com/gianarb/go-irc
with go.gianarb.it/irc
using GitHub Pages as an example:
- Create a project go-libraries
- Add a file with the same name as the project, irc , with the following contents:
1
2
3
4
5
6
7
8
9
|
<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>
|
- Bind the project to the domain: go.gianarb.it
- Download the dependency package using
go get go.gianarb.it/irc
5. Frequently Asked Questions
- SUM checksum error when
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
|