← Back to list

How to Publish a Go Module to GitHub and Import It in Your Project

Go modules are the standard way to manage dependencies and share reusable code in Go. If you want other projects to import your code…

Christian · 2025-12-20 04:46 · 0 claps · 1.7 min read
#go-modules #golang #shared-libraries
Open on Medium ↗
Wiki topics: 🔓 · Open Source

How to Publish a Go Module to GitHub and Import It in Your Project

Go modules are the standard way to manage dependencies and share reusable code in Go. If you want other projects to import your code directly from GitHub, you need to structure and publish your module correctly.

This guide walks through creating a Go module, pushing it to GitHub, and importing it into another project using best practices.

What Is a Go Module?

A Go module is a versioned collection of Go packages identified by a module path, usually a GitHub repository URL. Go modules solve:

  • Dependency versioning
  • Reproducible builds
  • Code reuse across projects

How to create a Go Module

Step 1: Initialize the Go module using your GitHub repository path:

This creates a go.mod file that defines your module path and Go version.

go mod init github.com/yourusername/go-utils

Step 2: Write the Go Code you want to put

Add the reusable code you want to share. For example, the following helper functions standardize API responses when using Fiber:

func Fail(ctx *fiber.Ctx, code int, message string) error {
 return ctx.Status(code).JSON(ResponseDTO{
  Status:  "fail",
  Code:    code,
  Message: message,
  Errors:  []interface{}{message},
 })
}

func Success(ctx *fiber.Ctx, code int, message string) error {
 return ctx.Status(code).JSON(ResponseDTO{
  Status:  "success",
  Code:    code,
  Message: message,
 })
}

func SuccessWithData(ctx *fiber.Ctx, code int, message string, data interface{}) error {
 return ctx.Status(code).JSON(ResponseDTO{
  Status:  "success",
  Code:    code,
  Message: message,
  Data:    data,
 })
}

Step 3: Commit and Push to GitHub

Initialize a Git repository and push your code to GitHub:

git init
git add .
git commit -m "Initial Go module"
git branch -M main
git remote add origin https://github.com/yourusername/go-utils.git
git push -u origin main

Step 4: Tag a Version (Important)

Go modules rely on semantic versioning. Tagging a version allows other projects to depend on a stable release:

git tag v1.0.0
git push origin v1.0.0

Step 5: Import the Go Module from GitHub

In another Go project, fetch the module using:

go get github.com/yourusername/go-utils@v1.0.0

Example usage:

package main

import (
 "github.com/gofiber/fiber/v2"
 responseutils "github.com/yourusername/go-utils"
)

func main() {
 app := fiber.New()

 app.Get("/user/:id", func(c *fiber.Ctx) error {
  userID := c.Params("id")

  if userID == "" {
   return responseutils.Fail(
    c,
    fiber.StatusBadRequest,
    "user id is required",
   )
  }

  user := map[string]interface{}{
   "id":   userID,
   "name": "John Doe",
  }

  return responseutils.SuccessWithData(
   c,
   fiber.StatusOK,
   "user fetched successfully",
   user,
  )
 })

 app.Listen(":3000")
}

Please check out my go-fiber-utils GitHub module to see the full implementation.

https://github.com/sadstitch003/go-fiber-utils


메타데이터
post_id
fb97892e731b
slug
how-to-publish-a-go-module-to-github-and-import-it-in-your-project-fb97892e731b
url
https://medium.com/@christian.asterisk/how-to-publish-a-go-module-to-github-and-import-it-in-your-project-fb97892e731b
canonical_url
https://medium.com/@christian.asterisk/how-to-publish-a-go-module-to-github-and-import-it-in-your-project-fb97892e731b
author_url
https://medium.com/@christian.asterisk
status
ok
fetched_at
2026-06-27 18:20:27