接口中的动态返回类型

有以下代码:


用户模型.go


type UserModel struct{login, password}

var EntityArr = map[string]UserModel{}

func GetAll() map[string]UserModel {

    uuidString := uuid.New().String()

    user := Entity{Login: "myLogin", Password: "MyPassword"}

    EntityArr[uuidString] = user


    return EntityArr

}

PostModel.go


type PostModel struct{title, description}

func GetAll() map[string]PostModel {/* like in UserModel */}

用户控制器.go


type ControllerUser struct{}

func GetAll()map[string]UserModel { rertun UserModel.GetAll() } 

PostController.go


type ControllerPost struct{}

func GetAll()map[string]PostModel { rertun PostModel.GetAll() }

我尝试动态检测必要的控制器:


main.go


var currentController = Intfc

switch entity {

  "user" : currentController = UserController.ControllerUser{}

  "post" : currentController = PostController.ControllerPost{}

}

currentController.GetAll()

为此,我需要一个通用接口


接口.go


type Intfc interface {

    ShowAll() ???

}

函数(在接口中)返回什么类型的返回数据?


慕哥6287543
浏览 184回答 1
1回答

互换的青春

我做到了!@leafbebop、@Flimzy 和其他人期待https://github.com/arturturundaev/simpleCRUDproject
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go