Ad Space — Top Banner

type does not implement interface

Go Programming Language

Severity: Moderate

What Does This Error Mean?

This Go error means you are passing a value type (T) where the interface requires methods that are defined on a pointer type (*T). In Go, methods with pointer receivers (*T) are only in the method set of *T, not T. So if an interface requires a method defined with a pointer receiver, you must pass a pointer to satisfy the interface. The fix is to pass &myValue instead of myValue when assigning to the interface.

Affected Models

  • Go 1.20+
  • Go 1.21+
  • Go 1.22+
  • go build
  • Any Go project

Common Causes

  • An interface is satisfied by methods with pointer receivers (*T), but a value (T) is being assigned to the interface variable
  • A struct implements an interface using pointer receiver methods, but it is passed by value to a function expecting the interface
  • The method set of the value type T does not include methods defined on *T (only *T's method set includes pointer receiver methods)
  • A slice or map of interface types is populated with values instead of pointers to values that implement the interface
  • A type is returned by value from a function but needs to be returned as a pointer to satisfy the interface at the call site

How to Fix It

  1. Change the value to a pointer when assigning to the interface. If you have var w io.Writer = MyWriter{} and MyWriter uses pointer receivers, change it to var w io.Writer = &MyWriter{}

    The & operator takes the address of a value and gives you a pointer. *MyWriter's method set includes both pointer and value receiver methods.

  2. When passing to a function that expects an interface, pass a pointer. Change myFunc(writer) to myFunc(&writer) if writer is a value type and the method is defined on *MyWriter.

    If you are creating the value inline: myFunc(&MyWriter{}) instead of myFunc(MyWriter{})

  3. Review your method declarations. If you want both T and *T to satisfy an interface, use value receivers (func (m MyWriter) Write(...)) instead of pointer receivers. Value receiver methods are in the method set of both T and *T.

    Choose pointer receivers when the method needs to modify the struct or when the struct is large (avoids copying). Choose value receivers for read-only methods.

  4. Use a compile-time interface check to catch this error earlier. Add this line to your code: var _ io.Writer = (*MyWriter)(nil) — this checks at compile time that *MyWriter implements io.Writer without creating a real value.

    This pattern is common in Go library code. It documents the intended interface implementation and ensures the check fails at compile time if it breaks.

  5. Check Go's method set rules: A value T has access to value receiver methods only. A pointer *T has access to both value receiver methods AND pointer receiver methods. An interface is satisfied if the method set of the type includes all the interface's methods.

    Memorize this rule: if any method in the interface requires a pointer receiver, you must use a pointer to satisfy the interface.

When to Call a Professional

This is a Go type system and interface implementation error — no external service is needed. Understanding Go's pointer vs value receiver rules resolves this error. The fix is usually a one-character change: adding & before the value.

Frequently Asked Questions

Why does Go have this pointer vs value receiver distinction?

Go's design requires that calling a method on a value always works correctly. If a value receiver method is called on an addressable value, Go automatically takes its address. But Go cannot do this automatically when assigning to an interface, because the interface might store the value by copy. If Go allowed assigning a non-pointer value to an interface that requires pointer methods, the pointer methods would operate on a copy — which is almost never what you intend.

Should I always use pointer receivers to be safe?

Using pointer receivers for all methods on a type ensures the type satisfies any interface when used as *T. However, it is not always the right choice. Value receivers are appropriate for small, immutable types (like int-like types) where copying is cheap. Pointer receivers are appropriate when methods modify the struct or when the struct is large. For interface satisfaction, consistency within a type is most important — mix pointer and value receivers on the same type only intentionally.

What does the full error message look like in Go?

The full error is typically: 'cannot use myValue (type MyWriter) as type io.Writer in assignment: MyWriter does not implement io.Writer (Write method has pointer receiver)'. The key information is at the end: 'Write method has pointer receiver' — this tells you that the Write method was defined with *MyWriter as the receiver, not MyWriter. The fix is to use &myValue (a pointer) instead of myValue.