SOLVED: function main is undeclared in the main package (Go)
The Go linker error "function main is undeclared in the main package" stops your program from building or running. It looks alarming but almost always has a simple cause. This guide covers every scenario with code examples and fixes.
The Full Error Message
When you run go build or go run, you may see:
# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package
Or, when using go run on a specific file:
$ go run main.go
# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package
The linker is telling you that it compiled a package declared as main, but could not find a func main() entry point to link against. Without main(), the Go runtime does not know where execution should start.
Cause 1: Wrong Package Declaration
Every Go executable must have exactly one file with package main at the top. If the package is declared with a different name, the compiler treats it as a library package — and library packages do not need (or provide) a main function.
Example of the bug
// main.go
package myapp // <-- wrong: should be "package main"
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
$ go run main.go
# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package
Fix
Change the package declaration to package main:
// main.go
package main // <-- correct
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
$ go run main.go
Hello, world!
Cause 2: Running go run on the Wrong File
go run file.go compiles only the named file. If func main() lives in a different file (e.g. cmd.go) and you run go run main.go, Go compiles only main.go and never sees the main function.
Example of the bug
project/
main.go ← package main, imports, helpers
cmd.go ← package main, func main() { ... }
$ go run main.go
# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package
Fix
Use go run . to compile all .go files in the current directory together:
go run .
Or list all files explicitly:
go run main.go cmd.go
For packages in subdirectories:
go run ./cmd/myapp/
As a rule, prefer go run . over go run filename.go unless you have a single-file program.
Cause 3: func main() is Missing Entirely
The function may simply not exist — it was deleted, never written, or accidentally renamed.
Example of the bug
// main.go
package main
import "fmt"
func run() { // <-- named "run" instead of "main"
fmt.Println("Hello, world!")
}
$ go run .
# command-line-arguments
runtime.main_main·f: function main is undeclared in the main package
Fix
Add func main() and call your logic from it:
// main.go
package main
import "fmt"
func run() {
fmt.Println("Hello, world!")
}
func main() {
run()
}
The Go specification requires that func main() takes no arguments and returns no values. The following signatures are all wrong and will cause the error:
func main(args []string) { } // wrong: no parameters allowed
func Main() { } // wrong: Go is case-sensitive; must be lowercase
func MAIN() { } // wrong
The correct signature is always:
func main() {
// entry point
}
Cause 4: Build Tags Excluding the File
Go build tags (also called build constraints) can cause the file containing func main() to be silently excluded from the build. When that happens, the compiler sees a package main with no main function.
Example of the bug
// main.go
//go:build ignore
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
The //go:build ignore tag tells the Go toolchain to skip this file entirely during normal builds. The file is present on disk but is invisible to go build and go run.
Another common pattern that causes the same problem:
//go:build linux
package main
func main() { }
If you build this on macOS or Windows without specifying GOOS=linux, the file is excluded and main disappears.
Fix
Remove the build tag if it was added by accident:
// main.go
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}
Check which files are being included in the current build:
go list -f '{{.GoFiles}}' .
Check which files are being excluded and why:
go list -f '{{.IgnoredGoFiles}}' .
Override the OS/arch constraint when building for a specific target:
GOOS=linux go build .
List all build constraints in a file by inspecting the top comment block:
head -5 main.go
Build tag lines must appear before the package declaration and before any blank lines, like this:
//go:build linux && amd64
package main
Quick Checklist
Run through these steps if you see "function main is undeclared in the main package":
- [ ] Does the file with
func main()start withpackage main(notpackage myappor any other name)? - [ ] Are you using
go run .rather thango run specificfile.go? - [ ] Does a
func main()with exactly that signature (no parameters, no return value) exist somewhere in the package? - [ ] Is the function named
mainin lowercase (notMainorMAIN)? - [ ] Does the file have a
//go:build ignoretag or an OS/arch tag that excludes it on your current platform? - [ ] Run
go list -f '{{.GoFiles}}' .— is the file containingfunc main()in the output list?