How to initialize values for nested struct array in golang

前端 未结 3 1594
鱼传尺愫
鱼传尺愫 2021-02-12 18:04

My struct

type Result struct {
   name   string
   Objects []struct {
       id int
   }
}

Initialize values for this

func mai         


        
3条回答
  •  旧巷少年郎
    2021-02-12 18:45

    Objects contains no elements. You need to append element first. Like this:

    r.Objects = append(r.Objects, struct{ id int }{}) 
    

    Also you can omit r.Objects[0].id = 10; using initialization of your struct like this:

    r.Objects = append(r.Objects, struct{ id int }{ 10 })
    

提交回复
热议问题