I'm wondering if there is a way to dynamically expand the number of entries that share the same data types in a struct without using an array.
for example:
type MyHouse struct{
Bedroom *Bedroom `json:"bedroom"`
Kitchen *Kitchen `json:"Kitchen"`
}
type Kitchen struct{
Sink *Sink `json:"sink"`
Oven *Oven `json:"oven"`
}
type Oven struct{
Brand string `json:"brand"`
HobSize []int `json:"hobs"`
type Sink struct{
Volume int `json:"volume"`
}
type Bedroom struct{
Bed *Bed `json:"bed"`
Table *Table `json:"table"`
}
type Bed struct{
Brand string `json:"brand"`
Size String `json:"size"`
type Table struct{
Brand string `json:"brand"`
Legs int `json:"legs"`
SurfaceArea int `json:"SurfacceArea"`
Draws []Draws`json:"draws"`
}
type Draws struct{
Depth int `json:"depth"`
Width int `json:"width"`
Length int `json:"length"`
}
func main() {
res := &MyHouse{
Bedroom: &Bedroom{
Bed: &Bed{
Brand: "Sleepy",
Size : "King"},
Table: &Table{
Brand : "TabelY"
Legs : 1
SurfaceAr: 32
Draws : []Draws{
{
Depth :10
Width :10
Length :10
},
{
Depth :20
Width :10
Length :10
}
}
}
}
Kitcken: &Kitchen{
Oven: &Oven{
Brand: "Bake right",
hobs: []int{12,12,24,24}
}
Sink: &Sink{
Volume: 56
}
}
}
restopring, _ := json.Marshal(res)
fmt.Println(string(resprint))
}
This is fine to simply print out exactly what is there, and I could just exchange the hard-coded values for variables (I didn't just to keep this simple) if I want to simply be able to alter the values.
what I want to be able to do is add another table to the bedroom with the exact same data (different values) without turning the tables into an array of tables (and actually the same for the rooms) so that I could print out a json that was formatted something like:
{MasterBedroom:(all relevant stuff)},{SpareBedroom:(its own set of stuff)}
Also, this is just an example, in reality, It may be buildings with 100s of rooms each with a lot of furniture all requiring different names but sharing the same basic data defined in the structs. I was thinking about some kind of for loop that might take a user value for how many bedrooms a user had and dynamically create that many bedrooms in the json like:
{Bedroom1:(all relevant stuff)},{Bedroom2:(its own set of stuff)}.... etc
I've seen a couple examples that use the map but they end up printing out:
map[[Bedroom1:[all stuff]],Bedroom2:[its stuff]]
which also doesn't work the way I need it to
Thank you in advance
To clarify in the example I have given you would need to be able to make an infinite number of rooms inside the json without using []bedrooms in the my house struct
This is not the same as the json marshal example some people have posted that has user:Frank as an example