I am writing microservices in Go. After some coding, I decided to give a more dynamic approach for my querying system, but I am having some issues.
What I would like to do, is call a function from a file that will query the database, set the data to the Model, and return the result. By this, I mean something like the schema below
// database_manager.go
func DbCall(query string, TableModel interface{}) interface{} {
db, _ := sqlx.Connect(/*All my parameters and so on */)
model := []TableModel{}
err := db.Select(&model, query)
if err != nil {
return nil
}
return model
}
// /blog/models/blog_articles.go
type BlogArticles struct {
Article_id int `json:"article_id"`
Title string `json:"title"`
Text string `json:"text"`
}
// /blog/services/blog_articles.go
func CustomService() []BlogArticles {
table = []BlogArticles
data := DbCall("SELECT * FROM blog_articles", &table)
return data
}
However, this approach does return different errors, ranging from "non-struct dest type interface with >1 columns (3)" to "expected slice but got interface"
this and this solutions did not help me.
I have looked at different answers around StackOverflow, but none really solve my issue.
So I thought that my issue is either unsolvable or poorly approached or that I am just making a mess.
I would like to ask those with more experience if what I am thinking and trying to do is feasible, possible, and a good way to have a dynamic approach to my goal.
To sum up a little:
Does my idea make sense and is it feasible?
If so, how could I potentially pass a Model from one function to another and use it in this dynamic way?
Eventually, is this approach correct? Should I change my way of approaching this? Is there a better way to have my code rather dynamic?
Thanks for the help and for reading thus far