package main import "fmt" // 测试 type API interface { Test() string } type APICall struct { // 接口 a AmoudleAPI b BmoudleAPI } func (api *APICall) Test() string { return fmt.Sprintf("%s\n%s", api.a.TestA(), api.b.TestB()) } func NewApi() API { return &APICall{NewAmoudleAPI(), NewBmoudleAPI()} } //A测试 type AmoudleAPI interface { TestA() string } type aMoudleImpl struct{} func (*aMoudleImpl) TestA() string { return "主网服务已运行" } func NewAmoudleAPI() AmoudleAPI { return &aMoudleImpl{} } //B测试 type BmoudleAPI interface { TestB() string } type bMoudleImpl struct{} func (*bMoudleImpl) TestB() string { return "测试网服务已运行" } func NewBmoudleAPI() BmoudleAPI { return &bMoudleImpl{} } func main() { api := NewApi() fmt.Println(api.Test()) }