How To Use Struct In Go Lang?
In Go, a struct is a composite data type that allows you to group together values of different data types into a single entity. Structs are a powerful tool that can be used in a variety of ways in Go, including creating custom data types, representing objects, and passing data between functions. In this article, we'll explore the different scenarios and uses of structs in Go with examples.
Struct Syntax:
To create a struct in Go, you use the type
keyword, followed by the name of the struct and its fields. Here's the syntax:
type StructName struct {
Field1 FieldType1
Field2 FieldType2
...
}
In the above syntax, StructName
is the name of the struct, and Field1
, Field2
, etc. are the names of the fields. FieldType1
, FieldType2
, etc. are the types of the fields.
Creating a Struct:
To create a struct in Go, you use the type
keyword, followed by the name of the struct and its fields. Here's an example:
type Person struct {
name string
age int
}
In the above example, we define a Person
struct with two fields, name
and age
. The name
field is of type string
, and the age
field is of type int
.
Initializing a Struct:
You can initialize a struct in Go using a struct literal. Here's an example:
person := Person{name: "John", age: 30}
In the above example, we initialize a Person
struct with the name "John" and age 30.
Accessing Struct Fields:
You can access the fields of a struct in Go using the dot notation. Here's an example:
fmt.Println(person.name) // "John"
fmt.Println(person.age) // 30
In the above example, we access the name
and age
fields of the person
struct.
Passing Structs to Functions: You can pass structs to functions in Go just like any other variable. Here's an example:
func printPerson(person Person) {
fmt.Printf("Name: %s, Age: %d\n", person.name, person.age)
}
func main() {
person := Person{name: "John", age: 30}
printPerson(person)
}
In the above example, we define a printPerson()
function that takes a Person
struct as a parameter and prints its fields.
Embedding Structs:
In Go, you can embed one struct inside another to create a new struct with additional fields. Here's an example:
type Address struct {
street string
city string
country string
}
type Person struct {
name string
age int
address Address
}
func main() {
person := Person{
name: "John",
age: 30,
address: Address{
street: "123 Main St",
city: "New York",
country: "USA",
},
}
fmt.Printf("Name: %s, Age: %d, Street: %s, City: %s, Country: %s\n",
person.name, person.age, person.address.street, person.address.city, person.address.country)
}
In the above example, we define an Address
struct with three fields, street
, city
, and country
. We then define a Person
struct that embeds the Address
struct as a field. We initialize a Person
struct with a name, age, and address, and then access its fields using the dot notation.
Conclusion:
Structs are a powerful tool in Go that can be used in a variety of scenarios and use cases. Whether you're creating custom data types, representing objects, or passing data between functions, structs offer a flexible and efficient way to organize and manipulate data in your Go programs. By understanding how to create, initialize, access, and pass structs to functions, you can take full advantage of the power and flexibility of this important Go feature.