Go

Fix Go "Cannot Assign To ..." Compile Errors — Addressability, Map Values, Constants, and Call Results

An assignment needs a place to put the value, not just a name to put it under. A variable has a place; a constant does not, a call result does not, and a field of a value stored in a map does not. The compiler checks the left-hand side before it checks the value, so a perfectly good right-hand side still fails when the target cannot hold it. Each section below takes one shape, shows the exact message it prints, and gives the fix that belongs to that shape.

Written November 19, 2026 · Tested with Go 1.26.4 · 20 min read

By the end of this guide
  • The first error: a field of a struct stored in a map
  • The increment that compiles: a map index with ++
  • A constant is not a variable
  • A call result has nowhere to store

What this guide is

cannot assign to is the compiler saying the left-hand side of an assignment has no storage behind it. The specification's rule for assignments is one sentence: each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier. A variable is addressable — it names storage. A map index expression is the one exception: m["a"] = 2 is legal even though a map index is not addressable, because the map itself does the storing. Everything else on the left has to be addressable, and the shapes in this guide are the ones that are not.

Most of the messages carry the reason in parentheses at the end: (neither addressable nor a map index expression). That parenthetical is the whole diagnosis in one phrase — the target is neither of the two things an assignment target is allowed to be. One shape in this guide breaks the pattern: a field of a struct stored in a map prints a message of its own, cannot assign to struct field m["a"].X in map, with no parenthetical at all. And one related shape is not an assignment error at all: calling a pointer method on a map value prints cannot call pointer method Inc on Counter, because the method needs an address the map value cannot give it.

The examples here use Go 1.26.4. Every example lives in a single file named main.go inside one module, created once from an empty directory:

Terminal
$ mkdir assign
$ cd assign
$ go mod init example.com/assign
go: creating new go.mod: module example.com/assign

Each program that follows replaces the contents of that main.go. When a build fails, the compiler prints the module path as a header line above the error — # example.com/assign here.

The first error: a field of a struct stored in a map

The most common shape stores a struct in a map and then tries to change one field of the stored struct in place:

Go
// main.go
package main

import "fmt"

type Point struct{ X, Y int }

func main() {
    m := map[string]Point{"a": {1, 2}}
    m["a"].X = 9
    fmt.Println(m["a"])
}
Terminal
$ go build
# example.com/assign
./main.go:9:2: cannot assign to struct field m["a"].X in map

The message is the special form, and it says exactly where the storage problem is: the field X belongs to a Point that lives inside the map, and a map holds copies of its values, not addresses of them. m["a"] returns a copy of the stored Point; assigning to a field of that copy would change nothing in the map, so the compiler refuses it outright rather than silently discarding the write. The position, line 9 column 2, is the start of the assignment statement. The fix is to copy the value out, change the copy, and store it back — the whole struct, which is a legal map-index assignment:

Go
// main.go
package main

import "fmt"

type Point struct{ X, Y int }

func main() {
    m := map[string]Point{"a": {1, 2}}
    v := m["a"]
    v.X = 9
    m["a"] = v
    fmt.Println(m["a"])
}
Terminal
$ go run main.go
{9 2}

The copy-out works because v is an ordinary variable, so v.X = 9 has storage behind it, and m["a"] = v is a map index expression on the left, which the assignment rule allows. The alternative fix is to store pointers in the map, map[string]*Point, so the map holds the address of the struct and a field assignment reaches the struct itself — that form is shown in the pointer-method section below, where the same choice decides whether a method call works.

The increment that compiles: a map index with ++

Before going further, the counter-example that catches people out: incrementing a map value in place compiles, even though assigning to a field of a map value does not. The program below runs and prints 2:

Go
// main.go
package main

import "fmt"

func main() {
    counts := map[string]int{"a": 1}
    counts["a"]++
    fmt.Println(counts["a"])
}
Terminal
$ go run main.go
2

This is not an inconsistency. The specification's rule for ++ and -- is that the operand must be addressable or a map index expression — the same two categories the assignment rule allows, minus the blank identifier. counts["a"] is a map index expression, so it qualifies as the operand of ++ directly, and the increment writes the new value back into the map. The field case fails because m["a"].X is neither: it is a field selector applied to a map index expression, and a field selector is only addressable when it is applied to something addressable. The line is drawn at the .X, not at the map. A whole-value assignment to a map index is legal for the same reason — m["a"] = 2 replaces the stored value, and the program below prints 2:

Go
// main.go
package main

import "fmt"

func main() {
    m := map[string]int{"a": 1}
    m["a"] = 2
    fmt.Println(m["a"])
}
Terminal
$ go run main.go
2

A constant is not a variable

A constant has a value but no storage: it is a name bound to a value at compile time, and there is nowhere for a new value to go. Reassigning one fails with the parenthetical form:

Go
// main.go
package main

import "fmt"

func main() {
    const limit = 10
    limit = 20
    fmt.Println(limit)
}
Terminal
$ go build
# example.com/assign
./main.go:7:2: cannot assign to limit (neither addressable nor a map index expression)

The parenthetical states the diagnosis: limit is neither addressable nor a map index expression. The fix is to declare the name with := instead of const when the value has to change, limit := 10 followed by limit = 20, which prints 20. If the value is genuinely meant to be fixed, the fix is the other way round: leave the constant alone and change the code that tried to reassign it. The compiler cannot tell which side is wrong — a constant reassigned by mistake and a variable declared const by mistake print the same message — so the decision is the author's: does this name change, or does it not?

A call result has nowhere to store

A function call returns a value, not a place. Assigning to the call's result fails the same way a constant does:

Go
// main.go
package main

import "fmt"

func get() int { return 1 }

func main() {
    get() = 2
    fmt.Println(get())
}
Terminal
$ go build
# example.com/assign
./main.go:8:2: cannot assign to get() (neither addressable nor a map index expression)

The message names the call itself, get(), as the thing that cannot be assigned to. The value 1 that get returns has no variable behind it to update, and a later call to get would return 1 again regardless. The fix is to store the result in a variable first and assign to that: n := get() followed by n = 2, which prints 2. The same shape appears with any expression that produces a value without storage — a conversion, a built-in call — and those are collected in their own section below.

A field of a composite literal

A composite literal, Point{1, 2}, is a value written out in full, and it is not addressable either. Selecting a field of one and assigning to it fails:

Go
// main.go
package main

import "fmt"

type Point struct{ X, Y int }

func main() {
    Point{1, 2}.X = 9
    fmt.Println("done")
}
Terminal
$ go build
# example.com/assign
./main.go:8:2: cannot assign to Point{…}.X (neither addressable nor a map index expression)

Note the message's rendering: the literal is abbreviated to Point{…}, with an ellipsis standing in for the values, because the message names the expression's shape rather than repeating its contents. The fix is the same as for a call result — give the value a name first: p := Point{1, 2} followed by p.X = 9, which prints {9 2}. The literal itself is fine as a value; it is only as an assignment target that it fails, because a literal is a value with no address.

A character of a string

A string's characters are not writable, and indexing a string does not produce an addressable byte. Assigning to one fails:

Go
// main.go
package main

import "fmt"

func main() {
    s := "hello"
    s[0] = 'H'
    fmt.Println(s)
}
Terminal
$ go build
# example.com/assign
./main.go:7:2: cannot assign to s[0] (neither addressable nor a map index expression)

The contrast here is with a slice, whose elements are addressable: s[0] on a []int is addressable, and the slice-increment program below shows it by printing [2 2]. A string is immutable — its bytes are fixed once the string exists — so s[0] is a value read out of the string, not a place in it. The fix is to convert the string to a mutable form, change it there, and convert back: r := []rune("hello"), r[0] = 'H', fmt.Println(string(r)), which prints Hello. The []rune form matters when the text may hold characters wider than one byte; for plain ASCII, []byte works the same way.

A field of a call's return value

The call-result shape and the field-selector shape combine: a function returns a struct by value, and the program tries to change a field of the returned struct in place:

Go
// main.go
package main

import "fmt"

type Point struct{ X, Y int }

func origin() Point { return Point{1, 2} }

func main() {
    origin().X = 9
    fmt.Println(origin())
}
Terminal
$ go build
# example.com/assign
./main.go:10:2: cannot assign to origin().X (neither addressable nor a map index expression)

The message names the whole target expression, origin().X: the call returns a copy of a Point, the field selector takes a field of that copy, and the copy has no address to write through. The fix, again, is to name the value first: o := origin() followed by o.X = 9, which prints {9 2}. If the function returned a pointer instead, *Point, the field assignment would work directly, because the pointer gives the address of a struct that does have storage — the same pointer-versus-value choice that decides the map case above.

The pointer method on a map value

A method with a pointer receiver needs the address of its receiver, and a map value cannot supply one. The program below fails, but not with an assignment message:

Go
// main.go
package main

import "fmt"

type Counter struct{ n int }

func (c *Counter) Inc() { c.n++ }

func main() {
    m := map[string]Counter{"a": {}}
    m["a"].Inc()
    fmt.Println(m["a"])
}
Terminal
$ go build
# example.com/assign
./main.go:11:9: cannot call pointer method Inc on Counter

The message is cannot call pointer method Inc on Counter: the method Inc has a pointer receiver, so calling it requires &m["a"], and a map index expression has no address to take. The specification's rule is that a reference to a method with a pointer receiver using an addressable value automatically takes the address of that value — t.Mp is equivalent to (&t).Mp — and a map value is not addressable, so the automatic step cannot happen. A value method would have compiled here: a value receiver takes a copy, and a copy is all a map index can give. The fix that keeps the mutation is to store pointers in the map, so the map holds an address and the pointer method has a receiver to work on:

Go
// main.go
package main

import "fmt"

type Counter struct{ n int }

func (c *Counter) Inc() { c.n++ }

func main() {
    m := map[string]*Counter{"a": {}}
    m["a"].Inc()
    fmt.Println(m["a"].n)
}
Terminal
$ go run main.go
1

The same copy-out pattern applies here: copy the Counter out, call Inc on the copy, and store it back. Which fix to choose depends on whether other code shares the value — pointers share one struct, copies do not — and that sharing decision is the author's, not the compiler's.

A conversion or a built-in call as the target

The call-result shape has two more spellings, both of which print the parenthetical form. A type conversion produces a value, not a place:

Go
// main.go
package main

import "fmt"

func main() {
    x := 1.5
    int(x) = 2
    fmt.Println(x)
}
Terminal
$ go build
# example.com/assign
./main.go:7:2: cannot assign to int(x) (neither addressable nor a map index expression)

And a built-in call is a call like any other for this purpose:

Go
// main.go
package main

import "fmt"

func main() {
    s := "hello"
    len(s) = 3
    fmt.Println(s)
}
Terminal
$ go build
# example.com/assign
./main.go:7:2: cannot assign to len(s) (neither addressable nor a map index expression)

Both fixes are the same: store the result in a variable and assign to the variable. int(x) never changes x — a conversion makes a new value of the target type and leaves the operand untouched — so assigning to it would be meaningless even if it compiled. len(s) is a count computed from the string, not a field of it; the way to change the length is to change the string, not the count.

The exact rule: what counts as addressable

The specification's assignment rule, read in full, is the whole guide in one paragraph: each left-hand side operand must be addressable, a map index expression, or (for = assignments only) the blank identifier, and operands may be parenthesized. The examples the specification gives for legal targets are x = 1, *p = f(), and a[i] = 23 — a variable, a dereferenced pointer, and an index expression. The IncDec rule is the same two categories: the operand of ++ or -- must be addressable or a map index expression.

What that means in practice, against the shapes above:

  • A variable is addressable. A dereferenced pointer, *p, is addressable, because the pointer names the storage. A slice element, s[0], is addressable, because the slice names the storage behind its elements. An array element of an addressable array is addressable. A field of an addressable struct is addressable — v.X works when v is a variable, and fails when v is a map value, a call result, or a literal, because the thing the field hangs off has no address.
  • A map index expression is the exception, not an addressable thing: it is allowed as a target because the map performs the store, and it is allowed as an IncDec operand for the same reason. But the exception stops at the index: a field selector on a map index, m["a"].X, is neither addressable nor a map index expression, and neither is a pointer method called on one.
  • A constant, a call result, a conversion result, and a composite literal are values with no address. A string index is a value read out of an immutable string.

The two programs that show the addressable side of the rule both compile and run. A slice element increments in place:

Go
// main.go
package main

import "fmt"

func main() {
    s := []int{1, 2}
    s[0]++
    fmt.Println(s)
}
Terminal
$ go run main.go
[2 2]

And a dereferenced pointer takes an assignment:

Go
// main.go
package main

import "fmt"

func main() {
    n := 1
    p := &n
    *p = 2
    fmt.Println(n)
}
Terminal
$ go run main.go
2

The fixed program

The program below applies every fix at once: the map field is changed through a copy-out, the constant becomes a variable, the call results are stored before they are assigned to, the literal is named, the string goes through a rune slice, the map holds pointers for its pointer method, and the slice element and the dereferenced pointer are assigned to directly:

Go
// main.go
package main

import "fmt"

type Point struct{ X, Y int }

type Counter struct{ n int }

func (c *Counter) Inc() { c.n++ }

func get() int { return 1 }

func origin() Point { return Point{1, 2} }

func main() {
    m := map[string]Point{"a": {1, 2}}
    v := m["a"]
    v.X = 9
    m["a"] = v
    fmt.Println(m["a"])

    limit := 10
    limit = 20
    fmt.Println(limit)

    n := get()
    n = 2
    fmt.Println(n)

    p := Point{1, 2}
    p.X = 9
    fmt.Println(p)

    r := []rune("hello")
    r[0] = 'H'
    fmt.Println(string(r))

    o := origin()
    o.X = 9
    fmt.Println(o)

    counts := map[string]int{"a": 1}
    counts["a"]++
    fmt.Println(counts["a"])

    cm := map[string]*Counter{"a": {}}
    cm["a"].Inc()
    fmt.Println(cm["a"].n)

    s := []int{1, 2}
    s[0]++
    fmt.Println(s)

    x := 1
    px := &x
    *px = 2
    fmt.Println(x)
}
Terminal
$ go run main.go
{9 2}
20
2
{9 2}
Hello
{9 2}
2
1
[2 2]
2

The output is {9 2}, 20, 2, {9 2}, Hello, {9 2}, 2, 1, [2 2], and 2. Each line exercises one fix: the map field changes through the copy-out; the renamed constant takes its new value; the stored call result takes its assignment; the named literal takes its field change; the rune slice turns hello into Hello; the stored return value takes its field change; the map counter increments in place; the pointer method on the pointer map prints 1; the slice element increments; and the dereferenced pointer takes its assignment.

The fixes, ranked

Copy the map value out, change it, and store it back — or store pointers. This is the fix for the map shapes, which are the most common cause of this error in real code. Copy-out is the smaller change when the map is already built; storing pointers is the better design when the values are mutated often or shared, because it makes the field assignment and the pointer method both work directly. Pick one per map and keep it consistent — mixing value maps and pointer maps for the same data is how the error comes back.

Give the value a name before assigning to it. This is the fix for the call result, the literal, and the return-value field: n := get() then n = 2, p := Point{1, 2} then p.X = 9, o := origin() then o.X = 9. A name is a variable, and a variable is addressable. If the function returned a pointer, no naming is needed — the pointer already carries the address.

Change the declaration when the target is a constant. This is the fix when the name is meant to change: const becomes :=. When the name is meant to stay fixed, the fix is the opposite — leave the constant and change the code that tried to reassign it.

Convert the string, then convert back. This is the fix for a string character: []rune for text that may hold wide characters, []byte for plain ASCII, change the element, and convert back with string(...). There is no in-place edit of a string, because a string has no writable storage.

Do not assign to a conversion or a built-in call. A conversion makes a new value and leaves its operand alone; a built-in call computes a value from its argument. Assign to a variable that holds the result, or change the operand the result was computed from.

Which fix for which cause

What the code shows Cause Fix
A struct field in a map The map holds copies, not addresses Copy out, change, store back
counts["a"]++ None — a map index is a legal operand Nothing to fix; it compiles
A constant A constant has a value, not storage Declare it with :=, or stop
A call result A call returns a value, not a place Store the result, then assign
A literal's field A literal is a value with no address Name the literal first
A string character A string is immutable Convert, change, convert back
A return value's field The call returns a copy with no address Store the return value first
Pointer method on a map value The method needs an address Store pointers, or copy out
A conversion A conversion makes a new value Assign to a variable instead
A built-in call The call computes a value Change the argument instead
A map index None — a map index is an allowed target Nothing to fix
A slice element or *p None — both are addressable Nothing to fix

Checklist

  • Read the parenthetical first: (neither addressable nor a map index expression) means the target is neither of the two things an assignment target may be, and the fix is to make it one of them — a variable, a dereferenced pointer, a slice element, a field of an addressable struct, or a bare map index.
  • Watch for the map-field special form: cannot assign to struct field m["a"].X in map has no parenthetical, and it means the field hangs off a map value. The fix is copy-out-and-store-back, or pointers in the map.
  • Check whether the left-hand side is a whole map index before assuming it fails: m["a"] = 2 and counts["a"]++ are both legal, because a map index expression is an allowed target and an allowed IncDec operand. The error starts at the . after the index, not at the index itself.
  • Check whether the target is a value or a place: a constant, a call result, a conversion, a literal, and a string index are values. A variable, a dereferenced pointer, and a slice element are places. The fix for a value on the left is to store it in a variable first.
  • Check the receiver before calling a method on a map value: a value method works on the copy, a pointer method needs an address the map cannot give, and the message for that shape is cannot call pointer method, not cannot assign to.
  • When the count of arguments and the types are both right and the call still fails, the defect may be the target, not the call.

Sources

  • The Go Programming Language Specification — for assignment statements (each left-hand side operand must be addressable, a map index expression, or, for = assignments only, the blank identifier; operands may be parenthesized; x = 1, *p = f(), and a[i] = 23 are given as examples), for IncDec statements (the ++ and -- statements increment or decrement their operands by the untyped constant 1, and as with an assignment the operand must be addressable or a map index expression), and for method calls (a reference to a non-interface method with a pointer receiver using an addressable value automatically takes the address of that value, so t.Mp is equivalent to (&t).Mp; the result of a call such as makeT() is not addressable).

Keep Exploring