AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
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) } ~~~