Go Program to Create and Initialize a Struct

Introduction

In Go, a struct is a composite data type that groups together variables under a single name. These variables, known as fields, can have different types. Structs are commonly used to represent data structures and objects. This guide will demonstrate how to create and initialize a struct in Go.

Problem Statement

Create a Go program that:

  • Defines a struct type with multiple fields.
  • Creates and initializes an instance of the struct.
  • Displays the values of the struct fields.

Example:

  • Struct: Person with fields Name (string), Age (int), and City (string).
  • Output: Name: John Doe, Age: 30, City: New York

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. Display the Values of the Struct Fields: Use fmt.Println or fmt.Printf to display the 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 Create and Initialize a Struct
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 2: Create and initialize an instance of the struct using a struct literal
    person := Person{
        Name: "John Doe",
        Age:  30,
        City: "New York",
    }

    // Step 3: Display the values of the struct fields
    fmt.Printf("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. For example:
    person := Person{
        Name: "John Doe",
        Age:  30,
        City: "New York",
    }
    

    This initializes the Name, Age, and City fields of the person instance.

Step 3: Display the Values of the Struct Fields

  • The values of the struct fields are displayed using fmt.Printf with a formatted string:
    fmt.Printf("Name: %s, Age: %d, City: %s\n", person.Name, person.Age, person.City)
    

Output Example

Example Output:

Name: John Doe, Age: 30, City: New York

Example with Different Values:

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

Output:

Name: Alice Smith, Age: 25, City: San Francisco

Example Using Anonymous Struct:

You can also create an anonymous struct without defining a named type:

person := struct {
    Name string
    Age  int
    City string
}{
    Name: "Jane Doe",
    Age:  28,
    City: "Los Angeles",
}
fmt.Printf("Name: %s, Age: %d, City: %s\n", person.Name, person.Age, person.City)

Output:

Name: Jane Doe, Age: 28, City: Los Angeles

Conclusion

This Go program demonstrates how to create and initialize a struct using a struct literal. It covers basic programming concepts such as defining struct types, creating struct instances, and accessing struct fields in Go. This example is useful for beginners learning Go programming and understanding how to use structs to model complex data.

Leave a Comment

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

Scroll to Top