Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Go, interface implementation is implicit. A concrete type automatically satisfies an interface if its method set is a superset of the interface’s method set. This means the concrete type must include at least all the methods declared by the interface, and those specific method signatures (name, parameters, and return types) must match exactly. There is no explicit declaration of intent, such as an implements keyword. The relationship between the type and the interface is established entirely through structural typing and is verified by the compiler.

Mechanics of Implicit Implementation

To implement an interface, a type must define the required methods. The concrete type is permitted to have additional methods not defined by the interface, but it must possess the exact signatures of the interface’s methods to satisfy it.
// 1. Define the interface
type Formatter interface {
    Format() string
}

// 2. Define a concrete type
type Document struct {
    Content string
}

// 3. Define the method on the concrete type
// Document now implicitly implements Formatter
func (d Document) Format() string {
    return "<doc>" + d.Content + "</doc>"
}
The compiler enforces this relationship only when a concrete type is assigned to a variable of the interface type, passed as an argument to a function expecting the interface, or returned from a function returning the interface.
package main

import "fmt"

// Assuming Formatter and Document are defined as above

func PrintFormatted(f Formatter) {
    fmt.Println(f.Format())
}

func main() {
    doc := Document{Content: "Hello"}
    
    // The compiler verifies Document implements Formatter here
    PrintFormatted(doc) 
    
    // Or during direct assignment (using the blank identifier to avoid an unused variable error)
    var _ Formatter = doc 
}

Method Sets and Receiver Types

The most critical technical constraint of Go’s implicit implementation revolves around method sets and receiver types (value vs. pointer). The method set of a type determines which interfaces it implements.
  • The method set of a value type T consists only of methods declared with a value receiver (t T).
  • The method set of a pointer type *T consists of methods declared with both a pointer receiver (t *T) and a value receiver (t T).
If an interface is implemented using a pointer receiver, the value type does not implicitly implement the interface.
package main

type Mutator interface {
    Mutate()
}

type State struct {
    Value int
}

// Implemented via pointer receiver
func (s *State) Mutate() {
    s.Value++
}

func main() {
    var m Mutator
    
    // VALID: *State implements Mutator
    m = &State{Value: 1} 
    m.Mutate() // Use the variable to satisfy the compiler
    
    // INVALID: State (value) does not implement Mutator
    // m = State{Value: 1} // Compiler error: State does not implement Mutator (Mutate method has pointer receiver)
}

Static Interface Assertions

Because implementation is implicit, a type might accidentally drop support for an interface if a method signature is altered. To enforce a strict compile-time check without relying on usage in the business logic, Go developers use a static assertion using the blank identifier (_).
package main

type Processor interface {
    Process(data []byte) error
}

type ImageProcessor struct{}

func (ip *ImageProcessor) Process(data []byte) error {
    return nil
}

// Static assertion: Forces the compiler to verify that *ImageProcessor implements Processor.
// If the signature of Process changes, compilation will fail here.
var _ Processor = (*ImageProcessor)(nil)

func main() {
    // Application logic
}
In this construct, (*ImageProcessor)(nil) creates a typed nil pointer, which is then assigned to the blank identifier of the interface type Processor. This incurs no runtime overhead while guaranteeing structural compliance at compile time.
Master Go with Deep Grasping Methodology!Learn More