Hi, Silbert, can you clarify what you mean about managing structs? Are you asking about how to store/retrieve them in/from a database, how to keep the source code organized for all those structs, etc.?
When fetching data from a SQL database you need to have a struct. OK? If there are hundreds of SQL with different structs it seems overwhelming to put 100+ structs in one main.go. I had the thought to store the structs together with the query, but it seem that is a one way street, as the struct must be at global level.
So I am looking for a way to manage hundreds of structs in order to maintain them in an easy way.
If it is possible to store the struct together with the query it should be easy to maintain. I think…
This is not a requirement, though it makes things easier…
It seems overwhelming to have more than roughly 100 lines of code in main.go in general… Usually I just prepare the environment and then call into other packages, or start go routines which do the real heavy lifting.
Just organize your data in packages, or omit the structs at all when a simple single value return is sufficient for a query…
How many struct types you have in your problem domain? 100 seems a little excessive. Anyway if the number of structs grow you can always split it out into a separate file and put every function related to that struct inside that file. First organize your structs into files as your main.go grows, then organize your structs into packages as your number of files grow
As we said already, there is no need to use a struct at all for queries. And if you really do not want to use structs, then you can still use map[string]interface{}, but you really do not want that, using that is much worse than creating the structs you need to do it proper.
You could probably auto-generate the structs for them. Either by using something like db2struct or whipping up your own.
Instead of creating a separate a separate struct for joins, you can opt to include a member of the struct that points to the struct representing the joining table(s).
Let’s say you have a User struct for the users table, a Profile struct for profiles table
type User struct {
Username string
Profile *Profile
}
type Profile struct {
About string
Owner *User
}
so joining the two and reading it into a struct would look like:
q := "SELECT users.username, profiles.about FROM users INNER JOIN profiles ON users.user_id=profiles.user_id"
rows, err := db.Query(q)
if err != nil {
fmt.Println(err)
}
defer rows.Close()
list := make([]User, 0)
for rows.Next() {
u := User{Profile: &Profile{}}
err := rows.Scan(&u.Username, &u.Profile.About)
if err != nil {
fmt.Println(err)
}
list = append(list, u)
}
err = rows.Err()
if err != nil {
fmt.Println(err)
}
return list
[quote=“ditchx, post:11, topic:17473”]
You could probably auto-generate the structs for them. Either by using something like db2struct or whipping up your own.[/quote]
Thank you for the tips. You can group my problem into four:
Create Structs ( like db2struct? )
Store Structs ( packages? )
make([]Struct) ( 100 Structs =100 queries? )
Scan Structs ( 100 queries? )
I think sqlx will solve point 4, but I have not yet evaluated it…