Go: ./main.go:12:2: undefined: handler
The compiler cannot find an identifier — a typo, a missing import, or it lives in another file/package that wasn't built.
undefined: handlerundefined: x./main.go: undefined:undefined: fmtundefined: strings Meaning
Check spelling and case (exported names start with an uppercase letter). With go run main.go only that file is compiled; use go run . so other files in the package are included. Verify the import path for package-qualified names.
Common causes
- Typo or wrong capitalisation
- Function defined in another file but running
go run main.go - Missing import for the package
- Variable declared in an inner scope
- Build tags excluding the file
⚡ Quick fix
- Run
go run .orgo build ./...instead of one file - Add the missing import
- Fix the name's capitalisation
Detailed fix by platform
Go
go vet ./...andgo build ./...show all undefined names in the module
How to diagnose
- Command — One file or package?
- Name — Case and spelling?
- Import — Present?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- assignment mismatch: 1 variable but f returns 2 values Go: assignment mismatch: 1 variable but os.Open returns 2 values
- cannot assign to struct field m[k].f in map Go: cannot assign to struct field users["bob"].Age in map
- cannot refer to unexported name Go: cannot refer to unexported name pkg.helper (undefined: pkg.helper)
Most viewed in Go
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 4 Nov 2026