Cannot Call Pointer Method on Value

cannot call pointer method Write on MyWriter

Quick Answer

The method has a pointer receiver but you are calling it on a non-addressable value. Use a pointer to the value instead.

Why This Happens

When a method is defined with a pointer receiver (*T), it can only be called on a pointer or an addressable value. If you have a non-addressable value like a map element or function return value, you cannot call pointer methods on it directly.

The Problem

type MyWriter struct{}

func (w *MyWriter) Write(data []byte) {}

func getWriter() MyWriter {
    return MyWriter{}
}

func main() {
    getWriter().Write([]byte("hello")) // cannot take address of return value
}

The Fix

func main() {
    w := getWriter()
    w.Write([]byte("hello")) // w is addressable
}

Step-by-Step Fix

  1. 1

    Identify the receiver type

    Check if the method is defined on a pointer receiver (*T) or value receiver (T).

  2. 2

    Find the non-addressable value

    Determine why the value cannot have its address taken (map element, function return).

  3. 3

    Store in a variable

    Assign the value to a variable first so it becomes addressable, then call the method.

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