This is a code-sample detailing how to reference custom (and sometimes private/organisational) GO packages - hosted in gitLab/GitHub - within another separate project.
I've simply included both projects into 1, separated by a single folder called "Code that will ref our package", for easier reading,
but these projects dont need to be in the same directory.
Most solutions will simply have you copy/download the files into your work folder,
but this shows you how to avoid that, and simply reference the existing remote package
basically, just follow these steps: (more details are inside the actual main.go code file)
- write the code you need, following the package you want to import - and all its inputs
- run the "GOPRIVATE" command
- run the "go get" command
- run go mod init {file name}.go
- run go mod tidy -> this fetches the dependencies and links them up
- happy coding!
follow the steps above, or make sure your pipeline is following them
- set up an "Access Token" (in gitlab) or "Personal Access Token" (in GitHub -> you can limit permissions, etc.) for the repo you want to reference in your project
- set up a "variable" (in Gitlab) or "secret" (in GitHub) on your CICD pipeline/main repo
- place the value of Step 1's "Access Token" into the "Variable" in Step 2
- ensure that your CICD pipelines are using Step 2's variable/secret to access the repository (in addition to running the commands above)