How to created nested struct

I Want to create nested struct. I don’t know the depth of my child object .

Example : I want to create menus at runtime no limit for child.

Example
Root
M1
M11
M111
M12
M121
M2
M21
M211
M2111
M212
M2121
M3
M4

How achieve this

Is not really clear what you want but Id say something like

type MenuItem struct {
  ID int
  Title string
  Children []*MenuItem
}

type Menu struct {
  ID int
  Title string
  Items []*MenuItem
}
1 Like

Yes I know but how to drill down to child level.

It sounds like you might be asking about recursion:

https://gobyexample.com/recursion

In this case recursively iterating over your Menu.Items.Children.

I know I have to use recursive , but my problem is

menu[0].item[0] how I can do it programmatically

I can define level up to 5

That’s… not a question as far as I can tell. Are you asking how to create/define your structs? Are you asking how to index into children? As long as you’re recursively displaying them it wouldn’t matter how many levels you have.

Yes. If you can help. It will be great

Hi ,

I have following table .
myid	parentid	menu_name
1	0	Video Players
2	0	Televisions
11	1	HD Video Player
12	1	CD player
13	1	USB player
21	2	SuperLCD 42
22	2	SuperLED 42
23	2	SuperLED 50
24	2	SuperLCD 55 (Not available)
25	2	SuperLCD 70
111	11	Video1
112	11	Video2
1111	111	Video111
1112	111	Video112

I want this type of json object 

[{
  id: '1',
  name: 'Video Players',
  items: [{
    id: '1_1',
    name: 'HD Video Player',
    price: 220,
    icon: 'images/products/1.png',
  }, {
    id: '1_2',
    name: 'SuperHD Video Player',
    icon: 'images/products/2.png',
    price: 270,
  }],
}, {
  id: '2',
  name: 'Televisions',
  items: [{
    id: '2_1',
    name: 'SuperLCD 42',
    icon: 'images/products/7.png',
    price: 1200,
  }, {
    id: '2_2',
    name: 'SuperLED 42',
    icon: 'images/products/5.png',
    price: 1450,
  }, {
    id: '2_3',
    name: 'SuperLED 50',
    icon: 'images/products/4.png',
    price: 1600,
  }, {
    id: '2_4',
    name: 'SuperLCD 55 (Not available)',
    icon: 'images/products/6.png',
    price: 1350,
    disabled: true,
  }, {
    id: '2_5',
    name: 'SuperLCD 70',
    icon: 'images/products/9.png',
    price: 4000,
  }],
}, {
  id: '3',
  name: 'Monitors',
  items: [{
    id: '3_1',
    name: '19"',
    items: [{
      id: '3_1_1',
      name: 'DesktopLCD 19',
      icon: 'images/products/10.png',
      price: 160,
    }],
  }, {
    id: '3_2',
    name: '21"',
    items: [{
      id: '3_2_1',
      name: 'DesktopLCD 21',
      icon: 'images/products/12.png',
      price: 170,
    }, {
      id: '3_2_2',
      name: 'DesktopLED 21',
      icon: 'images/products/13.png',
      price: 175,
    }],
  }],
}, {
  id: '4',
  name: 'Projectors',
  items: [{
    id: '4_1',
    name: 'Projector Plus',
    icon: 'images/products/14.png',
    price: 550,
  }, {
    id: '4_2',
    name: 'Projector PlusHD',
    icon: 'images/products/15.png',
    price: 750,
  }],
}]

I have tried all option . I can’t able to created at runtime array with fields

In the data table it supposed exists, Price and Icon ?, I mean, the original data table struct is

type MenuItem struct {
  Id string
  ParentId string
  Name string
  Price float64
  Icon string 
}

If so, then you would need only to add items slice to that struct

type MenuItem struct {
  Id string
  ParentId string
  Name string
  Price float64
  Icon string 
  Items []MenuItem
}

Theere are some items with not price and icon (upper parent) so you can add some tags to your struct:

type MenuItem struct {
  Id       string `json:"id,omitempty"`
  ParentId string `json:"-"`
  Name     string     `json:"name,omitempty"`
  Price    float64    `json:"price,omitempty"`
  Icon     string     `json:"icon,omitempty"`
  Items    []MenuItem `json:"items,omitempty"`
}

No you would need a function that returns the items slice for a given id or an empty slice if the item does not has children.

func setChildren(menuItems []MenuItem, parentId string) []MenuItem) {
   result := make([]MenuItem,0)
   for _, mi := range menuItems {
      if mi.ParentId == parentId {
        result = append(result, mi)
      }	  
    }

    return result
}

HTH!!!