Anonymous interface implementation in Golang

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)