Apologies if this is wrong to begin with. I am attempting to learn Golang, and am working through various concepts. I am pretty sure I am confusing something here... I am trying to write a simple program that creates a struct called 'person' which accepts firstname, lastname, and age. I want a user to be able to specify the number of people they will be entering into a slice of struct person, then iterate through the number of people entered, taking the input and storing it in the slice of person. Then, I want to be able to print one of those 'person' to terminal, for instance the 6th one (assuming that many were entered). Here is what I have so far:
package main
import "fmt"
func main() {
var people int
type person struct {
firstName string
lastName string
age int
}
listOfPeople := make([]person, 10)
fmt.Println("Please enter the number of users you would like to create:")
fmt.Scan(&people)
for i := 0; i < people; i++ {
var a, b string
var c int
fmt.Println("Please enter a person's information: ")
fmt.Print("First Name: ")
fmt.Scan(&a)
fmt.Print("Last Name: ")
fmt.Scan(&b)
fmt.Print("Age: ")
fmt.Scan(&c)
listOfPeople = append(listOfPeople, person{a, b, c})
}
//Print the contents of listOfPeople
}
Where the code comment "print the contents of listOfPeople is where I would like to be able to print a 'person'. I have tried a couple of things but nothing prints or compilation fails. Without attempting to print out a person from listOfPeople the code compiles and runs. I know I am missing something here. Any help is appreciated. I realize this code is likely not optimized or there may be a simpler way of doing it, so if there is please feel free to correct me. Thank you