Golang: go get package from your private repo in Gitlab
BC
Posted on March 2, 2022
We can use go get
to install a module from our private repo.
Say we have a private repo gomath
that is hosted in gitlab under url: https://gitlab.com/yourname/gomath:
File math.go
:
package gomath
func Double(a int) int {
return a * 2
}
Now in your other Go module you want to install this package, you need to do 2 things:
- Set the
GOPRIVATE
env variable - Set
git config
to let git know your personal access token in gitlab. You can get the token from gitlab > preferences > Access Tokens. Make sure your token has the "read_repository" scope.
File config.sh
:
#!/usr/bin/env bash
go env -w GOPRIVATE=https://gitlab.com/yourname/gomath
git config \
--global \
url."https://<yourname>:<personal access token from gitlab>@gitlab.com".insteadOf \
"https://gitlab.com"
Run this script:
$ bash config.sh
Now under your Go module, you can just use go get
like normal:
$ go get https://gitlab.com/yourname/gomath
Then use it in main.go
:
package main
import (
"fmt"
"gitlab.com/yourname/gomath"
)
func main() {
num := 2
result := gomath.Double(num)
fmt.Println("result:", result)
}
💖 💪 🙅 🚩
BC
Posted on March 2, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.