I am using Golang Revel for some web project and I did like 12 projects in that so far. In all of them I have a lot of code redundancy because of return types. Look at this two functions:
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
}
As you can see they they both returns same type of data (type struct). My idea was just to pass string var like this:
func (c Helper) ReturnModels(modelName string) []*interface{} {
//do rethinkdb select with modelName and return []*interface{} for modelName
}
Like this I can have just one helper for returning data types instead of doing same thing over and over again for different models but same data type.
My questions are:
- Is this possible at all
- If yes can you point me to right docs
- If no, I will be more then happy to return your answer :)