In Go, c'è un modo per soddisfare un'interfaccia anonimamente? Non sembra che ci sia, ma questo è stato il mio miglior tentativo.Implementazione di un'interfaccia anonima in Golang
(Nella 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)
}
Questo è davvero impressionante, e mi piace come è semi-strutturata. L'incorporamento è davvero pulito. – jocull
All'inizio ho letto la parte "non possibile", ma poi sono tornato e in realtà l'ho eseguito! Ben fatto! –