-2

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

  • 1
    Asked and answered before here: https://stackoverflow.com/questions/8270816/converting-go-struct-to-json – Steve Wilhelm Feb 02 '19 at 17:51
  • 2
    Possible duplicate of [Converting Go struct to JSON](https://stackoverflow.com/questions/8270816/converting-go-struct-to-json) – Shalauddin Ahamad Shuza Feb 02 '19 at 18:03
  • 1
    I think this may be a duplication of Steve wilhelms answer, also that does not answer the question as if you refer to the example I've provided I've already done this. For this answer to be relevant there would have to be a struct that contained a value that was of the user type and I would have to be able to have multiple users without just declaring the type as []users or including every possible user in the users stuct explicitly – Richard Chester Feb 02 '19 at 18:58
  • Wondering why you do not want to use an array, slice, or map? – Steve Wilhelm Feb 02 '19 at 23:42
  • Basically because my program is to make a json and then feed it into an external api I have no control over, the api works fine if it's simple and want to write the json by hand but quickly gets complicated for bigger applications. That's why I'm trying to build a program to generate the json for me. – Richard Chester Feb 03 '19 at 07:40
  • I think I need to be able to change the Myhouse stuct at run time so that if the user enters two rooms it looks like `type Myhouse struct {Bedroom *Bedroom json:"bedroom", Bedroom1 *Bedroom json:"bedroom1"}` and if it was 3 bedrooms it world be `type Myhouse struct {Bedroom *Bedroom json:"bedroom", Bedroom1 *Bedroom json:"bedroom1",Bedroom2 *Bedroom json:"bedroom2" }` up to a theoretically Infinate amount of rooms – Richard Chester Feb 03 '19 at 07:56

1 Answers1

1

Asked and answered before here: Converting Go struct to JSON

// Annotate the struct with json tags
type Foo struct {
    Number int    `json:"number"`
    Title  string `json:"title"`
}
// ...
foo := &Foo{Number: 123, Title: "Bar"}
f, err := json.Marshal(foo)
Steve Wilhelm
  • 6,200
  • 2
  • 32
  • 36
  • This is just a small scale version of the example I provided and does not answer the question. Now if there was a value in another struct that pointed to the foo type and allowed me to call it multiple times without explicitly calling it in the struct like bar:{foo:{num:int, title:string}, anotherfoo:{num:int, title:string}} – Richard Chester Feb 02 '19 at 19:02