Hi, I wanted to define a struct in a package called structure
and than share an populate the struct from other files and functions. Let’s assume that I define a struct like this
package structure
type Try struct {
NameOne string
Street []string
}
where NameOne
is the name of a person like “Bob” and Steet
is the name of the street(s) where he has an house, assuming that there is the possibility that he has more than a house. than I’m trying to populate the struct from then main:
package main
import (
"./str"
"fmt"
)
func main(){
var list structure.Try
list.NameOne = "Joe"
list.Street = "Frist"
list.NameOne = "Frank"
list.Street = "Ninth"
fmt.Println(list)
}
but I obviously get just the last NameOne
and Street
, how can I get all the data? I also tried to declare var list []structure.Try
instead of var list structure.Try
but if I do that I get list.NameOne undefined (type []structure.Try has no field or method NameOne)
for all the list.* in the main func.
Graphical example after having added this data:
NameOne Street
--------------------------------------------------------
First street number 23
Bob Second street number 15
Ninth street number 9
--------------------------------------------------------
Jim Fifth street number 16
--------------------------------------------------------
so if I print Bob data I should get his name and three streets whereas if I print Jim I should get only his name and one street
Hope you can help,
Thanks