# How to created nested struct

**URL:** <https://forum.golangbridge.org/t/how-to-created-nested-struct/34914>\
**Category:** Getting Help\
**Created:** [March 26, 2024, 12:02pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914 "2024-03-26T12:02:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![packs](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/packs/32/9635_2.png) [@packs](https://forum.golangbridge.org/u/packs)\
**Post date:** [March 26, 2024, 12:02pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/1 "2024-03-26T12:02:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Massimo.Costa](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/massimo.costa/32/9802_2.png) [@Massimo.Costa](https://forum.golangbridge.org/u/Massimo.Costa)\
**Post date:** [March 26, 2024, 4:11pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/2 "2024-03-26T16:11:10Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![packs](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/packs/32/9635_2.png) [@packs](https://forum.golangbridge.org/u/packs)\
**Post date:** [March 26, 2024, 4:31pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/3 "2024-03-26T16:31:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dean\_Davidson](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dean_davidson/32/9685_2.png) [@Dean\_Davidson](https://forum.golangbridge.org/u/Dean_Davidson)\
**Post date:** [March 26, 2024, 5:38pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/4 "2024-03-26T17:38:08Z")

</div>

It sounds like you might be asking about recursion:

[https://gobyexample.com/recursion](https://gobyexample.com/recursion)

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

---

<div class="post-metadata">

**Author:** ![packs](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/packs/32/9635_2.png) [@packs](https://forum.golangbridge.org/u/packs)\
**Post date:** [March 27, 2024, 4:20pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/5 "2024-03-27T16:20:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dean\_Davidson](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/dean_davidson/32/9685_2.png) [@Dean\_Davidson](https://forum.golangbridge.org/u/Dean_Davidson)\
**Post date:** [March 27, 2024, 5:11pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/6 "2024-03-27T17:11:36Z")

</div>

> [@packs](#):
>
> I know I have to use recursive , but my problem is
> 
> menu[0].item[0] how I can do it programmatically

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.

---

<div class="post-metadata">

**Author:** ![packs](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/packs/32/9635_2.png) [@packs](https://forum.golangbridge.org/u/packs)\
**Post date:** [March 27, 2024, 5:54pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/7 "2024-03-27T17:54:08Z")

</div>

Yes. If you can help. It will be great

---

<div class="post-metadata">

**Author:** ![packs](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/packs/32/9635_2.png) [@packs](https://forum.golangbridge.org/u/packs)\
**Post date:** [March 31, 2024, 9:53am UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/8 "2024-03-31T09:53:37Z")

</div>

Hi ,

```auto
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

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [March 31, 2024, 4:21pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/9 "2024-03-31T16:21:47Z")

</div>

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!!!

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [June 29, 2024, 4:22pm UTC](https://forum.golangbridge.org/t/how-to-created-nested-struct/34914/10 "2024-06-29T16:22:34Z")

</div>

Closed on June 29, 2024, 4:22pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
