Share array of structs between packages and functions

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

Show us this code.

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)
}
func main() {

	var list = new(structure.Try)

	list.NameOne = "Joe"
	list.Street = make([]string, 2)
	list.Street[0] = "Frist"
	list.NameOne = "Frank"
	list.Street[1] = "Ninth"

	for key, v := range list.Street {
		fmt.Println(key, ' ', v)
	}

}
2 Likes

Thanks, @BigBoulard, just last the last thing since, I do not at prior how many streets there will be (you wrote 2 in the make function), how can I solve this, I mean it could be 1, 5, 0 or any other number. Is there anyway to declare it and then append to it as many values, as they are? And right now, if I’m not wrong, you are overwriting the name of Joe with Frank, instead I would like to create an array of the struct I wrote up, so that I can print Joe and all the streets, or Frank and all his streets.

Hi @wecandoit

Sorry, I’ve read too fast… here it should be a bit better

func main() {
	var list []*structure.Try

	var contact1 = new(structure.Try)
	contact1.NameOne = "Joe"
	contact1.Street = make([]string, 1)
	contact1.Street[0] = "Frist"
	contact1.Street = append(contact1.Street, "Strauss", "Blair", "Oxford")
	list = append(list, contact1)

	var contact2 = new(structure.Try)
	contact2.NameOne = "Frank"
	contact2.Street = make([]string, 1)
	contact2.Street[0] = "Ninth"
	contact2.Street = append(contact2.Street, "Washington", "McGregor", "O'Sullivan")
	list = append(list, contact2)

	for _, v := range list {
		fmt.Print((*v).NameOne, ": ")
		for _, v2 := range (*v).Street {
			fmt.Print(v2, " - ")
		}
		fmt.Println("")
	}
2 Likes