【发布时间】:2015-09-30 11:00:32
【问题描述】:
在 Go 中,有没有办法匿名满足接口?好像没有,但这是我最好的尝试。
(在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)
}
【问题讨论】:
标签: go anonymous-types