1
votes

We are developing a multi-tenant application with Go backend. We use Gorm as ORM library.

In some cases we have to assign some custom fields to certain users, so different structs for user X and user Y.

Is it possible to make structs dynamic?

1
No it's not possible, Go is a statically typed language. However, if you need to provide custom fields for a struct type you could have it have a field of type map/slice of CustomField... - mkopriva

1 Answers

1
votes

static language golang doesn't support dynamic struct. Instead, I sugguest using a higher struct User containing all fields X and Y both have. Besides, Whether to design a bigger struct or different structs depends on your db model. If there is only one table in the database t_user, one model is good. If there are two or more tables like t_X ,t_Y, apparently you should design two or more models.