Golang Program to Create a Slice of Person Structs Called People with at Least Two Elements


In this article, we are going to explore how to create a Go program that involves a slice of Person structs named People. The slice will contain a minimum of two elements, representing individuals with their respective names and addresses. By exploring the concept of slices and their usage in Go, we will learn how to effectively manage collections of data and perform operations on them. Let's dive in and explore how to work with slices in Go to handle a group of Person structs.Here we are going to use two different methods: using literal initialization and using the append function along with examples to elaborate the concept.

Syntax

person.name

It is used to access the name field of the variable person.

  • person − represent a variable of a specific type.

  • name − it represents the field name.

person.address

It is used to access the name field of the variable person.

  • person − represent a variable of a specific type.

  • address − it represents the field address.

Algorithm

  • Define a Person struct with name and address fields.

  • Create a slice called People of type []Person.

  • Initialize the People slice with at least two Person struct elements.

  • Verify the length of the People slice to ensure it has at least two elements.

  • Access and modify the elements of the People slice as needed.

  • Test the program to ensure the slice of Person structs is created correctly.

Example 1

In this code example, we define the Person struct with Name and Address fields.

Next, we initialize the People slice using literal initialization. Each element of the slice is a Person struct enclosed in curly braces {}. We provide the values for the Name and Address fields for each person within the braces. In the main function, we iterate over the people slice using a for-range loop. We access each person and print their name and address using the Printf function from the fmt package.

package main

import "fmt"

type Person struct {
   Name    string
   Address string
}

func main() {
   var people = []Person{
      {Name: "John Doe", Address: "123 Main Street"},
      {Name: "Jane Smith", Address: "456 Elm Avenue"},
   }

   for _, person := range people {
      fmt.Printf("Name: %s\n", person.Name)
      fmt.Printf("Address: %s\n", person.Address)
      fmt.Println()
   }
}

Output

Name: John Doe
Address: 123 Main Street

Name: Jane Smith
Address: 456 Elm Avenue

Example 2

In this code example, we first define the Person struct with Name and Address fields.

Next, we create an empty slice of Person structs using var people []Person.

Using the append function, we add Person structs to the people slice. Each append statement appends a new Person struct to the slice, specifying the values for the Name and Address fields.In the main function, we iterate over the people slice using a for range loop and print the name and address of each person using the Printf function.

package main

import "fmt"

type Person struct {
   Name    string
   Address string
}

func main() {
   var people []Person

   people = append(people, Person{Name: "John Doe", Address: "123 Main Street"})
   people = append(people, Person{Name: "Jane Smith", Address: "456 Elm Avenue"})

   for _, person := range people {
      fmt.Printf("Name: %s\n", person.Name)
      fmt.Printf("Address: %s\n", person.Address)
      fmt.Println()
   }
}

Output

Name: John Doe
Address: 123 Main Street

Name: Jane Smith
Address: 456 Elm Avenue

Conclusion

Creating a cut of Individual structs in Go permits you to store and control a collection of individual information productively. By taking after the calculation sketched out over, you'll be able effectively to make a cut named Individuals with at least two components. The capacity to work with cuts in Go gives adaptability and comfort when managing collections of information.

Updated on: 20-Jul-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements