How to manage hundreds of structs?

All examples are using at most 3 structs. I cannot find any example or tutorial that shows how to manage hundreds of structs.

type Person struct {
	FirstName, LastName string
	Age       int
}

I have managed to store and fetch many SQL queries in a database, but this does not seems to work with structs. Or?

TIA.

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…

Any idea?

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

What is the options? Beside more packages?

100 tables with different structs. And with queries using joins, I suppose at least twice as many.

I know I can use more packages, but what are the options?

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.

So for the example from your original post:

Can you show how you’re using this?

This is my simple struct:

type Rec struct {
	Id      int
	Subject string
}

And using the struct:

func Get(query string) interface{} {

	rows, err := db.Query(query)
	if err != nil {
		fmt.Println(err)
	}

	defer rows.Close()

	list := make([]Rec, 0)
	for rows.Next() {
		rec := Rec{}
		err := rows.Scan(&rec.Id, &rec.Subject)
		if err != nil {
			fmt.Println(err)
		}
		list = append(list, rec)
	}

	err = rows.Err()
	if err != nil {
		fmt.Println(err)
	}

	return list
}

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:

  1. Create Structs ( like db2struct? )
  2. Store Structs ( packages? )
  3. make([]Struct) ( 100 Structs =100 queries? )
  4. Scan Structs ( 100 queries? )

I think sqlx will solve point 4, but I have not yet evaluated it…