Go Program to Access and Modify Struct Fields

Introduction

In Go, structs are composite data types that group together fields under a single name. You can easily access and modify the fields of a struct after creating an instance of it. This guide will demonstrate how to access and modify struct fields in Go.

Problem Statement

Create a Go program that:

  • Defines a struct type with multiple fields.
  • Creates an instance of the struct.
  • Accesses and displays the initial values of the struct fields.
  • Modifies the values of some fields.
  • Displays the modified values of the struct fields.

Example:

  • Struct: Person with fields Name (string), Age (int), and City (string).
  • Output:
    Initial Values: Name: John Doe, Age: 30, City: New York
    Modified Values: Name: John Doe, Age: 31, City: Los Angeles
    

Solution Steps

  1. Define the Struct Type: Define a struct type with the required fields.
  2. Import the fmt Package: Use import "fmt" for formatted I/O operations.
  3. Write the Main Function: Define the main function, which is the entry point of every Go program.
  4. Create and Initialize an Instance of the Struct: Use a struct literal to create and initialize an instance of the struct.
  5. Access and Display the Initial Values of the Struct Fields: Use fmt.Println or fmt.Printf to display the initial values of the struct fields.
  6. Modify the Values of Some Fields: Assign new values to the fields of the struct.
  7. Display the Modified Values of the Struct Fields: Use fmt.Println or fmt.Printf to display the modified values of the struct fields.

Go Program

package main

import "fmt"

// Step 1: Define the struct type
type Person struct {
    Name string
    Age  int
    City string
}

/**
 * Go Program to Access and Modify Struct Fields
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 2: Create and initialize an instance of the struct
    person := Person{
        Name: "John Doe",
        Age:  30,
        City: "New York",
    }

    // Step 3: Access and display the initial values of the struct fields
    fmt.Printf("Initial Values: Name: %s, Age: %d, City: %s\n", person.Name, person.Age, person.City)

    // Step 4: Modify the values of some fields
    person.Age = 31
    person.City = "Los Angeles"

    // Step 5: Display the modified values of the struct fields
    fmt.Printf("Modified Values: Name: %s, Age: %d, City: %s\n", person.Name, person.Age, person.City)
}

Explanation

Step 1: Define the Struct Type

  • The struct type Person is defined with three fields: Name of type string, Age of type int, and City of type string. This struct can be used to represent a person’s basic information.

Step 2: Create and Initialize an Instance of the Struct

  • An instance of the Person struct is created and initialized using a struct literal:
    person := Person{
        Name: "John Doe",
        Age:  30,
        City: "New York",
    }
    

Step 3: Access and Display the Initial Values of the Struct Fields

  • The initial values of the struct fields are accessed and displayed using fmt.Printf:
    fmt.Printf("Initial Values: Name: %s, Age: %d, City: %s\n", person.Name, person.Age, person.City)
    

Step 4: Modify the Values of Some Fields

  • The values of specific fields in the struct can be modified by directly assigning new values:
    person.Age = 31
    person.City = "Los Angeles"
    

Step 5: Display the Modified Values of the Struct Fields

  • The modified values of the struct fields are accessed and displayed using fmt.Printf:
    fmt.Printf("Modified Values: Name: %s, Age: %d, City: %s\n", person.Name, person.Age, person.City)
    

Output Example

Example Output:

Initial Values: Name: John Doe, Age: 30, City: New York
Modified Values: Name: John Doe, Age: 31, City: Los Angeles

Example with Different Values:

person := Person{
    Name: "Alice Smith",
    Age:  25,
    City: "San Francisco",
}

After modifying:

person.Age = 26
person.City = "Seattle"

Output:

Initial Values: Name: Alice Smith, Age: 25, City: San Francisco
Modified Values: Name: Alice Smith, Age: 26, City: Seattle

Conclusion

This Go program demonstrates how to access and modify struct fields. It covers basic programming concepts such as defining struct types, creating and modifying struct instances, and accessing struct fields in Go. This example is useful for beginners learning Go programming and understanding how to work with structs to manage data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top