Why This Happens
Go initializes package-level variables in dependency order. If variable A's initializer references B and B's initializer references A, Go cannot determine the initialization order and reports an error. Break the cycle by computing one value in an init() function.
The Problem
var a = b + 1
var b = a + 1 // initialization loopThe Fix
var a int
var b int
func init() {
a = 1
b = a + 1
}Step-by-Step Fix
- 1
Identify the loop
Read the error to see which variables form the initialization cycle.
- 2
Break the dependency
Determine which variable should be initialized first.
- 3
Use init() function
Move one or both initializations into an init() function where you control the order.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free