function main is undeclared in the main package Go Origin: Application & API Last updated 4 Nov 2026

Go: runtime.main_main·f: function main is undeclared in the main package

The package is named main but contains no func main(), or you tried to build a library as an executable.

Exact message you may see function main is undeclared in the main packageruntime.main_main·f: function main is undeclared in the main packagego run: cannot run non-main packagepackage command-line-arguments is not a main package
Seen on: Go

Meaning

An executable needs package main with func main(). Libraries should use another package name. This error often appears when running go run in a directory containing only library code, or when main has the wrong signature or is in a _test.go file.

Common causes

  • No func main() defined
  • Package name main in a library directory
  • func main in a file excluded by build tags
  • Typo func Main()
  • Running go run in the wrong folder

⚡ Quick fix

  1. Add func main() {} in package main
  2. Rename the package for libraries
  3. Run from the correct directory: go run ./cmd/app

Detailed fix by platform

Go

  1. go build ./cmd/server for a repo with multiple commands

How to diagnose

  1. Package — main or library?
  2. Function — main() exists?
  3. Directory — Correct?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

assignment mismatch: 1 variable but f returns 2 values — Go: assignment mismatch: 1 variable but os.Open returns 2 values (Go)assignment mismatch: …cannot assign to struct field m[k].f in map — Go: cannot assign to struct field users["bob"].Age in map (Go)cannot assign to stru…cannot refer to unexported name — Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper) (Go)cannot refer to unexp…cannot use x as type in assignment/argument — Go: cannot use "5" (untyped string constant) as int value in argument to f (Go)cannot use x as type …cgo: C compiler gcc not found — Go: cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in $PATH (Go)cgo: C compiler gcc n…COPY failed — COPY failed: file not found in build context / "/app/x": not found (Docker)COPY faileddid not complete successfully — failed to solve: process "/bin/sh -c ..." did not complete successfully: exit code: 1 (Docker)did not complete succ…does not implement interface (missing method) — Go: cannot use &Dog{} (value of type *Dog) as Animal value: *Dog does not implement Animal (missing method Speak) (Go)does not implement in…function …

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.