undefined: x Go Origin: Application & API Last updated 4 Nov 2026

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.

Exact message you may see undefined: handlerundefined: x./main.go: undefined:undefined: fmtundefined: strings
Seen on: Go

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

  1. Run go run . or go build ./... instead of one file
  2. Add the missing import
  3. Fix the name's capitalisation

Detailed fix by platform

Go

  1. go vet ./... and go build ./... show all undefined names in the module

How to diagnose

  1. Command — One file or package?
  2. Name — Case and spelling?
  3. 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:

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 …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 main is undeclared in the main package — Go: runtime.main_main·f: function main is undeclared in the main package (Go)function main is unde…import cycle not allowed — Go: package app/models imports app/utils imports app/models: import cycle not allowed (Go)import cycle not allo…imported and not used — Go: ./main.go:5:2: "os" imported and not used (Go)imported and not usedundefined…

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.