gpt4 book ai didi

go - 接口(interface)中的动态返回类型

转载 作者:行者123 更新时间:2023-12-01 22:37:17 24 4
gpt4 key购买 nike

有以下代码:

UserModel.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 */}

UserController.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() }

我尝试动态检测必要的 Controller :

main.go
var currentController = Intfc
switch entity {
"user" : currentController = UserController.ControllerUser{}
"post" : currentController = PostController.ControllerPost{}
}
currentController.GetAll()

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

接口(interface).go
type Intfc interface {
ShowAll() ???
}

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

最佳答案

我做到了!
@leafbebop、@Flimzy 和其他人期待 https://github.com/arturturundaev/simpleCRUDproject

关于go - 接口(interface)中的动态返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59477648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com