Skip to main content
In this lesson, you’ll learn how to publish your Go module on GitHub. Follow the steps below to initialize your Git repository, commit your changes, tag your release, and update your module for public use.

Initialize Your Git Repository

Start by converting your unversioned project directory into a Git repository. Begin in your project’s root directory, as illustrated below:
The image shows an integrated development environment (IDE) with a project directory open, displaying folders and files related to encryption and decryption. The terminal at the bottom indicates the use of Go version 1.19.3.
Log into your GitHub account and create a new, empty repository. Copy the repository URL provided by GitHub for future use. Run the following command to initialize the Git repository:
Initializing the repository sets up the necessary metadata, transforming your project directory into a version-controlled environment.

Add Remote and Verify Configuration

Next, add a remote named “origin” that points to your GitHub repository:
Verify that the remote configuration is correct by listing all remotes:
You should see output similar to:

Check Repository Status

Check the current status of your repository. Here, a custom “go status” command (used to illustrate changes) triggers an error message since it’s not a valid Go command:
At this point, your project includes the encrypt and decrypt packages, along with the go.mod and main.go files. Stage and commit these files with an appropriate message:

Tagging Your Commit

For this demonstration, we’re releasing a minor version update. Tag your commit with a version number using the following command:
The output may look similar to:
List the tags to verify the new tag:

Push the Tag to GitHub

Push the tag to your GitHub repository:
After a successful push, you will see an output confirming that the objects have been uploaded:
All changes, including the tag, are now live on GitHub. Confirm this by visiting your repository:
The image shows a GitHub repository page for a project named "cryptit" with version v0.1.0. It includes folders for "decrypt" and "encrypt," and files "go.mod" and "main.go," with recent changes noted.

Update the Module Reference

Previously, you used a replace directive in your go.mod file to reference a local copy of the module:
Now that your module is published, remove the replace directive so that your go.mod file looks like this:

Fetch the Published Module

Use the go get command to retrieve the published module with its tag. Note that the full module path is required:
This updates your go.mod file to include the module as an indirect dependency:
The console output will confirm the update:

Update Your Application

Modify your main.go to import and use the now-published module. Below is an example of how your main.go might look:
Run your application using:
An example output might be:
This confirms that your application is now using the published module from GitHub instead of a local reference.

Final Verification

Ensure the go.mod file correctly reflects the change:
Run your program one more time:
The expected output should be:
With your module now published on GitHub, you can easily share and reuse your code. This practice simplifies dependency management and supports semantic versioning.
That concludes this lesson on publishing a module to GitHub. Happy coding, and we’ll see you in the next lesson!

Watch Video