Back to: Golang
As a language, Go provides support for the creation and use of structs. A struct is a composite data type that aggregates data of different types. This feature of Go allows you to create custom data types by grouping related fields together.
Declare a Struct
A struct type is defined using the type
keyword followed by the name of the struct and the fields it contains within curly braces. Each field is declared with a name and type. The values of the fields can be accessed using the dot operator.
Here is an example of a struct type:
type person struct {
name string
age int
}
You can create an instance of a struct by using the var
keyword followed by the name of the instance and the struct type. Then, you can set the values of the fields using the dot operator.
Here is an example of how to create an instance of a struct and set its values:
var p person
p.name = "John"
p.age = 30
Create a pointer to a struct using the new
keyword:
You can also create a pointer to a struct using the new
keyword. Then, you can set the values of the fields using the arrow operator.
A pointer is a variable that stores the memory address of another variable. Pointers are used to reference and manipulate the values of variables indirectly. When working with structs, pointers can be particularly useful to access and modify struct fields.
To create a pointer to a struct in Go, the ‘&’ operator is used followed by the name of the struct variable. This returns a pointer to the memory address of the struct variable. For example:
type Person struct {
Name string
Age int
}
func main() {
// create a struct variable
p1 := Person{"Alice", 25}
// create a pointer to the struct variable
p2 := &p1
// modify struct field using the pointer
p2.Age = 26
// print struct fields
fmt.Println(p1) // {Alice 26}
}
In this example, a Person
struct is defined with two fields: Name
and Age
. A new struct variable p1
is created and assigned values for the fields. A pointer to p1
is created by using the ‘&’ operator followed by p1
. The field Age
of p1
is modified indirectly through the pointer p2
. Finally, the modified p1
is printed which displays the updated Age
.
Access Struct Fields:
We can access the fields of a struct using the .
operator:
fmt.Println(emp.name)
fmt.Println(emp.age)
Code Example
Here is a code example on Go struct with output:
package main
import "fmt"
type person struct {
name string
age int
}
func main() {
// Initializing a struct with values
p1 := person{name: "John", age: 30}
fmt.Println(p1)
// Initializing a struct with zero values
var p2 person
fmt.Println(p2)
// Updating struct field values
p1.name = "Jane"
p1.age = 25
fmt.Println(p1)
}
Output:
{John 30}
{ 0}
{Jane 25}
In this example, we define a struct person
with two fields: name
of type string and age
of type int. In the main
function, we create an instance of the person
struct with the name
field set to “John” and the age
field set to 30. We print this instance using fmt.Println
.
We also create a second instance of the person
struct with the var
keyword, which initializes the fields with their zero values (an empty string for name
and 0 for age
). We print this instance as well.
Follow us on social media
Follow Author