多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
Go语言没有构造函数,所以一般会定义NewXXX函数来初始化相关类。 NewXXX函数返回接口时就是简单工厂模式 ~~~ package simplefactory import "fmt" type Api interface { Say(name string) string } func NewApi(t int) Api { if t == 1 { return &hiApi{} } else if t == 2 { return &helloApi{} } return nil } type hiApi struct { } func (*hiApi) Say(name string) string { return fmt.Sprintf("Hi, %s", name) } type helloApi struct { } func (*helloApi) Say(name string) string { return fmt.Sprintf("Hello, %s", name) } ~~~