Go complains "undefined" for a function from another file? Let's figure it out

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

"Undefined" function in Go: how to make files work together without losing your mind

\n\n

Imagine: you're writing a Go program, splitting the code into logical files (e.g., main.go and helper.go), run go run main.go — and the compiler gives you an icy: "undefined: MyFunction". Sound familiar? This question is one of the most popular on Stack Overflow (thousands of views!), and today we'll break it down once and for all.

\n\n

Why does Go, which is famous for its simplicity, suddenly not see a function from a neighboring file? It's not magic, but strict code organization rules. Let's go through all the scenarios — from the classic beginner mistake to proper module handling.

\n\n

Scenario 1: Classic — forgot to specify all files when running

\n\n

The most common case. You write:

\n\n
// main.go\npackage main\n\nfunc main() {\n    printHello() // Error: undefined: printHello\n}\n
\n\n
// helper.go\npackage main\n\nimport "fmt"\n\nfunc printHello() {\n    fmt.Println("Hello!")\n}\n
\n\n

And you only run go run main.go. Go doesn't see helper.go because you didn't explicitly specify it. The correct solution:

\n\n
go run main.go helper.go\n// or simpler:\ngo run .\n
\n\n

Why is that? Go does not automatically scan the directory — you must list all files of the package. The go run . command runs the entire current folder as a single package.

\n\n

Scenario 2: Different packages — invisibility by default

\n\n

Another popular pitfall is trying to call a function from another package without importing and exporting.

\n\n
// main.go\npackage main\n\nfunc main() {\n    helper.DoSomething() // Error: undefined: helper\n}\n
\n\n
// helper/helper.go\npackage helper\n\nfunc DoSomething() {\n    // ...\n}\n
\n\n

There are two problems here:

\n
    \n
  • The files are in different packages (main and helper).
  • \n
  • The DoSomething function is not exported (starts with a lowercase letter).
  • \n
\n\n

Corrected version:

\n\n
// main.go\npackage main\n\nimport "yourmodule/helper"\n\nfunc main() {\n    helper.DoSomething() // Works!\n}\n
\n\n
// helper/helper.go\npackage helper\n\nfunc DoSomething() { // Capital letter!\n    // ...\n}\n
\n\n

Remember: In Go, only entities with a capital letter are exported. A lowercase letter means private to the package.

\n\n

Scenario 3: Problems with modules and GOPATH

\n\n

If you're using the old style (GOPATH) or initialized the module incorrectly, Go might not find your files even in the same folder.

\n\n

Check:

\n
    \n
  1. Is there a go.mod file in the project root? If not, create one: go mod init module_name.
  2. \n
  3. Are you storing the project inside $GOPATH/src without a module? This is legacy mode that can cause confusion.
  4. \n
  5. Make sure all files in the folder have the same package (except test files with the _test suffix).
  6. \n
\n\n

Example of a correct structure:

\n\n
myproject/\n├── go.mod          # module myproject\n├── main.go         # package main\n└── helper.go       # package main\n
\n\n

Scenario 4: File names with underscores and test files

\n\n

Go ignores files starting with _ or . (e.g., _hidden.go). Files with the _test.go suffix are only built when running tests (go test). If you accidentally named a working file as helper_test.go — its functions will not be visible in the main code.

\n\n

Scenario 5: Circular dependencies

\n\n

Sometimes the "undefined" error can be a consequence of circular imports. For example, package A imports B, and B imports A. Go forbids such cycles and m

Blogs

Book Recommendations