Why This Happens
json.Unmarshal needs to modify the target variable, so it requires a pointer. If you pass a value, it cannot write the decoded data back. This applies to json.Unmarshal, json.NewDecoder().Decode(), and similar functions.
The Problem
type Config struct {
Port int `json:"port"`
}
func main() {
var config Config
data := []byte(`{"port": 8080}`)
json.Unmarshal(data, config) // needs pointer
}The Fix
func main() {
var config Config
data := []byte(`{"port": 8080}`)
err := json.Unmarshal(data, &config)
if err != nil {
log.Fatal(err)
}
fmt.Println(config.Port)
}Step-by-Step Fix
- 1
Identify the unmarshal call
Find the json.Unmarshal or Decode call that is not receiving a pointer.
- 2
Add the address operator
Add & before the target variable to pass a pointer.
- 3
Check the error
Always check the error return value from unmarshal operations.
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