./main.go:...: goes to bed
The “goes to bed” error is one of Go’s compiler easter eggs. It appears when you use a goto statement that references a missing or incorrectly scoped label. The phrase is a playful reference to the goto keyword — “goes to bed” rhymes with “goto”.
What It Means
When the Go compiler encounters a goto statement, it checks that the target label exists and is reachable according to Go’s scoping rules. If the label is missing or the jump crosses a variable declaration boundary, the compiler reports “goes to bed” — a whimsical way of saying the goto cannot find its target.
Why It Happens
- The label referenced by
gotodoes not exist in the function. - The label exists but is out of scope (e.g., defined in an inner block).
- The
gotojumps into a block that contains variable declarations, which Go forbids. - The
gotojumps over a variable declaration, creating a “jump over declaration” error. - The label name has a typo or does not match exactly (labels are case-sensitive).
How to Fix It
1. Ensure the label exists in the same function
Labels and goto must be in the same function scope:
func main() {
goto mylabel
// "goes to bed" — label does not exist
// Fix: define the label
mylabel:
fmt.Println("reached label")
}2. Do not jump over variable declarations
Go forbids goto that skips a variable declaration:
func main() {
goto end
x := 10 // jumped over — causes "goes to bed"
end:
fmt.Println("done")
}Fix by moving the declaration before the jump:
func main() {
x := 10
goto end
end:
fmt.Println(x)
}3. Define labels at the same block level
A label inside an inner block is not visible to outer goto statements:
func main() {
if true {
mylabel:
}
goto mylabel // "goes to bed" — label is in a different block
}Fix by moving the label to the same block or restructuring the logic.
4. Use structured control flow instead of goto
In most cases, you can replace goto with for, switch, or if:
// Instead of goto:
for i := 0; i < 10; i++ {
if condition {
goto exit
}
}
exit:
// Use a structured approach:
found := false
for i := 0; i < 10 && !found; i++ {
if condition {
found = true
}
}5. Check for exact label name matches
Labels are case-sensitive and must be followed by a colon:
// Wrong
goto MyLabel
mylabel: // different case
// Correct
goto MyLabel
MyLabel:FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro