我正在使用Golang Revel进行一些网络项目,并且目前为止我做了12个项目。在他们所有人中,由于返回类型,我有很多代码冗余。看看这两个函数:如何在Golang中返回动态类型结构?
func (c Helper) Brands() []*models.Brand{
//do some select on rethinkdb and populate correct model
var brands []*models.Brand
rows.All(&brands)
return brands
}
func (c Helper) BlogPosts() []*models.Post{
//do some select on rethinkdb and populate correct model
var posts []*models.Post
rows.All(&posts)
return posts
}
正如你所看到的,他们都返回相同类型的数据(类型struct)。 我的想法是只传递字符串VAR是这样的:
func (c Helper) ReturnModels(modelName string) []*interface{} {
//do rethinkdb select with modelName and return []*interface{} for modelName
}
喜欢我可以返回的数据类型,而不是为 不同型号,但相同的数据类型一遍又一遍做同样的事情又只是一个帮手。
我的问题是:
- 这是不可能的
- 如果是的,你可以点我正确的文档
- 如果没有,我会更快乐,然后回到你的答案:)
正轨@ pregmatch +1 –