golang接口使用
interface类型定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口
type Instance func() Interface
var adapters = make(map[string]Instance)
func Register(name string, adapter Instance) {
adapters[name] = adapter
}
func NewInstance(adapterName string) (adapter Instance, err error) {
adapter, ok := adapters[adapterName]
if !ok {
err = fmt.Errorf("instance: unknown adapter name %q (forgot to import?)", adapterName)
return
}
return
}
type Interface interface {
List()
Add()
Del()
Save()
}
func init(){
Register("l1",NewL1)
Register("l2",NewL2)
}
func NewL1() Instance{
return new(L1)
}
type L1 struct{}
func(l *L1)List(){}
func(l *L1)Add(){}
func(l *L1)Del(){}
func(l *L1)Save(){}
func NewL1() Instance{
return new(L1)
}
type L2 struct{}
func(l *L2)List(){}
func(l *L2)Add(){}
func(l *L2)Del(){}
func(l *L2)Save(){}