The Error
When you execute go build or go run, you encounter the following error:
runtime.main_main·f: function main is undeclared in the main package
This is a common Go compilation error that frustrates many developers, especially those new to Go. In this comprehensive guide, we'll explain exactly what causes this error and provide multiple solutions to fix it.
What Causes This Error?
The runtime.main_main·f: function main is undeclared in the main package error occurs when Go cannot find a valid entry point for your program. In Go, every executable program must have:
- A file with
package maindeclaration - A
func main()function in that package
The error typically happens in these scenarios:
Cause 1: Missing main Function
You have a file with package main but forgot to define the func main():
// main.go - WRONG: Missing main function
package main
import "fmt"
func sayHello() {
fmt.Println("Hello!")
}
Cause 2: Wrong Package Name
Your main file uses a different package name:
// main.go - WRONG: Should be "package main"
package myapp
func main() {
// This won't work as an entry point
}
Cause 3: Multiple Files with package main
When you have multiple .go files with package main, Go expects exactly ONE func main() across all of them. Having zero or multiple main functions causes issues.
Cause 4: Building a Library Instead of Executable
You're trying to go build on a package that's meant to be a library, not an executable.
Solution 1: Add the main Function
Make sure your main file has both the correct package declaration and the main function:
// main.go - CORRECT
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
sayHello()
}
func sayHello() {
fmt.Println("Hello from sayHello!")
}
Solution 2: Check Package Declaration
Verify that your entry point file uses package main:
// BEFORE (incorrect)
package myapplication
// AFTER (correct)
package main
Solution 3: Review All Files in the Directory
When you have multiple .go files, check each one:
# List all Go files in your directory
ls *.go
# Check the package declaration in each file
head -1 *.go
Make sure:
- All files in the same directory use the same package name
- Only ONE file has func main() if using package main
- Files intended as libraries use a different package name
Solution 4: Correct Project Structure
For a proper Go project structure:
myproject/
├── main.go # package main, contains func main()
├── handlers.go # package main, helper functions
└── utils/
└── helpers.go # package utils, library code
Example main.go:
package main
import (
"fmt"
"myproject/utils"
)
func main() {
fmt.Println("Starting application...")
utils.DoSomething()
}
Example handlers.go (same directory):
package main
import "fmt"
func handleRequest() {
fmt.Println("Handling request...")
}
Solution 5: Using go mod
If you're using Go modules (recommended for Go 1.11+), ensure your project is properly initialized:
# Initialize a new module
go mod init myproject
# Verify your go.mod file exists
cat go.mod
Your go.mod should look like:
module myproject
go 1.21
Verification Steps
After fixing the error, verify your code works:
# Build the project
go build
# Or run directly
go run main.go
# For multiple files
go run .
Common Mistakes to Avoid
-
Typo in main function name: It must be exactly
func main(), notfunc Main()orfunc MAIN() -
main function with parameters: The main function cannot take arguments: ```go // WRONG func main(args []string) {}
// CORRECT func main() {} ```
- main function with return value: The main function cannot return values: ```go // WRONG func main() int {}
// CORRECT func main() {} ```
- Using init() instead of main(): The
init()function runs before main, but you still need main: ```go package main
func init() { // Runs first }
func main() { // Entry point - REQUIRED } ```
Summary
The runtime.main_main·f: function main is undeclared error is straightforward to fix once you understand Go's requirements for executable programs:
- Use
package mainfor your entry point file - Define exactly one
func main()with no parameters and no return value - Keep all files in the same directory using the same package name
Following these guidelines ensures your Go programs compile and run correctly.