Importing Local Packages in Go: A Step-by-Step Guide with Code

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why does importing local packages in Go raise so many questions?

\n\n

If you've ever tried to organize Go code into separate files and folders, you've likely encountered an error like could not import "myproject/utils". This is one of the most common problems for beginner Go developers. On Stack Overflow, the question "How to import local packages in go?" has garnered thousands of views — and for good reason.

\n\n

Unlike Python, where you can simply place a file nearby and write import mymodule, Go requires a clear structure and proper module configuration. But once you master this mechanism, you'll understand how elegant it is. Let's figure it out once and for all.

\n\n

Brief Theory: How Go Understands Packages

\n\n

Before writing code, remember three key principles:

\n\n
    \n
  • Go is a language with a strict modular structure. Each directory is one package.
  • \n
  • The package name does not have to match the folder name, but it is strongly recommended for readability.
  • \n
  • Importing local packages since 2019 is done via modules (Go modules). Forget about GOPATH — use go mod.
  • \n
\n\n

Now let's move on to practice.

\n\n

Step 1: Create a Project with a Module

\n\n

Open your terminal and create a new folder for your project:

\n\n
mkdir go-local-imports\ncd go-local-imports\n
\n\n

Now initialize the module. The module name can be anything, but it's logical to use a path that will be unique (e.g., a link to a repository):

\n\n
go mod init github.com/your-username/go-local-imports\n
\n\n

After this command, a go.mod file will appear in the folder. This file will manage dependencies — both local and external.

\n\n

Step 2: Create a Local Package

\n\n

Let's create a utils folder inside the project. Inside it, create a file math.go:

\n\n
mkdir utils\ncd utils\ntouch math.go\n
\n\n

Open utils/math.go and write:

\n\n
package utils\n\n// Add returns the sum of two integers\nfunc Add(a, b int) int {\n    return a + b\n}\n
\n\n

Note: the function Add is written with a capital letter. In Go, this means it is exported (public). If you write add — it will be private and inaccessible from other packages.

\n\n

Step 3: Import and Use the Local Package

\n\n

Go back to the root folder of the project and create main.go:

\n\n
cd ..\ntouch main.go\n
\n\n

Contents of main.go:

\n\n
package main\n\nimport (\n    "fmt"\n    "github.com/your-username/go-local-imports/utils"\n)\n\nfunc main() {\n    result := utils.Add(3, 5)\n    fmt.Println("Sum:", result)\n}\n
\n\n

Now run the program:

\n\n
go run main.go\n
\n\n

You should see:

\n\n
Sum: 8\n
\n\n

Everything works! But if you got an error — read the next section.

\n\n

Common Errors and Their Solutions

\n\n

Error 1: "package is not in GOROOT"

\n\n

You see something like:

\n
package myproject/utils is not in GOROOT (/usr/local/go/src/myproject/utils)
\n\n

Cause: You are trying to import a package without a module. Go looks for it in the standard library, not in your project.

\n

Solution: Make sure there is a go.mod file in the project root. If not, run go mod init module-name.

\n\n

Error 2: "undefined: utils.Add"

\n\n

Cause: The function is written with a lowercase letter (private), or you imported the package but are not using its name.

\n

Solution: Check that all necessary functions are exported (with a capital letter), and that you are calling them

Blogs

Book Recommendations