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:
Personwith fieldsName(string),Age(int), andCity(string). - Output:
Name: John Doe, Age: 30, City: New York
Solution Steps
- Define the Struct Type: Define a struct type with the required fields.
- Import the fmt Package: Use
import "fmt"for formatted I/O operations. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Create and Initialize an Instance of the Struct: Use a struct literal to create and initialize an instance of the struct.
- Display the Values of the Struct Fields: Use
fmt.Printlnorfmt.Printfto 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
Personis defined with three fields:Nameof typestring,Ageof typeint, andCityof typestring. 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
Personstruct 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, andCityfields of thepersoninstance.
Step 3: Display the Values of the Struct Fields
- The values of the struct fields are displayed using
fmt.Printfwith 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.