Anonymous interface implementation in Golang

jocull picture jocull · Jul 11, 2015 · Viewed 35.9k times · Source

In Go, is there a way to satisfy an interface anonymously? It doesn't seem like there is, but this was my best attempt.

(In the Playground)

package main

import "fmt"

type Thing interface {
    Item() float64
    SetItem(float64)
}

func newThing() Thing {
    item := 0.0
    return struct {
        Item (func() float64)
        SetItem (func(float64))
    }{
        Item: func() float64 { return item },
        SetItem: func(x float64) { item = x },
    }
}

func main() {
    thing := newThing()
    fmt.Println("Hello, playground")
    fmt.Println(thing)
}

Answer

nemo picture nemo · Jul 12, 2015

Go uses method sets to declare which methods belong to a type. There is only one way to declare functions with receiver types (methods):

func (v T) methodName(...) ... { }

Since nested functions are forbidden, there is no way to define a method set on anonymous structs.

The second thing that will not allow this is that methods are read-only. Method values were introduced to allow to pass methods around and use them in goroutines but not to manipulate the method set.

What you can do instead is to provide a ProtoThing and refer to underlying implementations of your anonymous struct (on play):

type ProtoThing struct { 
    itemMethod func() float64
    setItemMethod func(float64)
}

func (t ProtoThing) Item() float64 { return t.itemMethod() }
func (t ProtoThing) SetItem(x float64) { t.setItemMethod(x) }

// ...

t := struct { ProtoThing }{}

t.itemMethod = func() float64 { return 2.0 }
t.setItemMethod = func(x float64) { item = x }

This works because by embedding ProtoThing the method set is inherited. Thus the anonymous struct also satisfies the Thing interface.