A
A
ajlhimik2019-06-26 23:10:14
go
ajlhimik, 2019-06-26 23:10:14

How to add data to an array of go structures?

type struct1 struct {
  NameList []struct2
}
type struct2 struct {
  ID int
  FuncList []struct3
}
type struct3 struct {
  Date []string
  Power int
  Intelect int
}

pers = struct1{}

previously unknown length of received data arrays, such as adding to pers:
pers.NameList[0].ID = 1
pers.NameList[1].ID = 2

because I can’t do it above, because the length of the array with the "append ()" function, I also can't because ...len(pers.NameList[0]) = 0
.\main.go:107:9: cannot use form["name_lucru"] (type []string) as type profList2 in append
.\main.go:107:9: append(prForm.Proflist, form["name_lucru"]) evaluated but not used

(Proflist is a NameList, and form["name_lucru"] will be 1 or 2 - the ID field, in my case form["name_lucru"] is also an array)
just in case, the original structure
type profList struct {
  Proflist []profList2 `json:"Proflist"`
}

type profList2 struct {
  NameProf string      `json:"NameProf"`
  OrarProf int16       `json:"OrarProf"`
  PersList []profList3 `json:"PersList"`
}

type profList3 struct {
  NameLucr    string `json:"NameLucr"`
  PrenumeLucr string `json:"PrenumeLucr"`
  Begconc1    string `json:"Begconc1"`
  Endconc1    string `json:"Endconc1"`
  Begconc2    string `json:"Begconc2"`
  Endconc2    string `json:"Endconc2"`
}

I understand that there is a type mismatch, but you can somehow increase the size of the pers.NameList array with nill values ​​​​for everyone and then assign everything you need

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2019-06-26
@ajlhimik

Is the idea clear?
https://play.golang.org/p/Qg9pe1K7X9I

spoiler
package main

import "fmt"

type struct1 struct {
  NameList []struct2
}
type struct2 struct {
  ID int
  FuncList []struct3
}
type struct3 struct {
  Date []string
  Power int
  Intelect int
}

func main(){
  Struct3 := struct3{}
  Struct3.Date = append(Struct3.Date, "date1", "date2") 
  Struct3.Date = append(Struct3.Date, "date3") 
  
  Struct2 := struct2{}
  Struct2.FuncList = append(Struct2.FuncList, Struct3)
  
  Struct1 := struct1{}
  Struct1.NameList = append(Struct1.NameList, Struct2)
  
  fmt.Printf("%+v", Struct1)
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question