Item #42: If the receiver is a map, why must the receiver be a value?

Author: Justme0Created Jan 2, 2026Updated Jan 2, 2026

Item 42 content:

A receiver must be a value

If we have to enforce a receiver’s immutability. If the receiver is a map, function, or channel. Otherwise, a compilation error occurs.

If the receiver is a map, why must the receiver be a value? Here is example:

https://go.dev/play/p/Fhle-hF1Uz2

go
package main

import "log"

type M map[int]int

func (m *M) foo() {
	log.Printf("len=%v.", len(*m))
}

func main() {
	var m M
	m.foo()
}

It is ok to compile and run the example.