How I used the private go module in my repo
In one of my recent projects, I aimed to create a Go module that would house a collection of common utility functions. This module was intended to be used across all repositories within our organization.
Creating go module
Creating a Go module is relatively straightforward; however, I won't focus on the creation process here, as numerous blogs cover that topic thoroughly. Instead, our primary challenge was figuring out how to use a module that we hosted privately on GitHub effectively.
When attempting to pull a Git module, you typically need to use SSH credentials or an authentication token. However, we prefer not to set up Git credentials on Docker or EC2 instances where our servers are launched.
To address this, I considered using Git submodules. Git submodules function as references to other Git repositories. By adding a repository as a submodule, it can be seamlessly pulled and integrated, allowing you to use its code as if it were part of your codebase.
Creating git submodule
Adding a submodule is pretty easy
git submodule add <repository-url> <localPathToSubmodule>
If you want your submodule to present in the current folder lib, then
git submodule add <repository-url> ./lib
Using git submodule as go module
Now, to use our repo as a go module, Just pull the code wherever you want
git submodule add <repository-url> ./lib
Now, to add this as a dependency to our project.
go mod edit -replace example.com/lib=./lib
go mod tidy
This approach allows us to add the module directly to our project, enabling easy usage without the need to set up any credentials on our Docker containers or EC2 instances. As a result, all private Go modules are readily available and seamlessly integrated.
Happy Coding ๐
Subscribe to my newsletter
Read articles from Dhairya Verma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Dhairya Verma
Dhairya Verma
Hey there ๐ I'm Dhairya, a backend developer. With a solid background spanning 5 years, my achievement includes scaling an app to 3M users.