JSON Unmarshal Requires a Pointer

json: Unmarshal(non-pointer main.Config)

Quick Answer

Pass a pointer to json.Unmarshal, not a value. Use &config instead of config.

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. 1

    Identify the unmarshal call

    Find the json.Unmarshal or Decode call that is not receiving a pointer.

  2. 2

    Add the address operator

    Add & before the target variable to pass a pointer.

  3. 3

    Check the error

    Always check the error return value from unmarshal operations.

Bugsly 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