Pointer to Struct in Golang
Overview
The struct keyword in golang, also known as "structure", is a user-defined data type that allows us to combine or group items of different types into a single type. Any real-world entity that has some set of properties and fields can be represented as a struct.
Introduction to the Struct in Golang
The struct keyword in golang, also known as structure, is a user-defined data type that allows us to combine or group items of different types into a single type. It is typically compared with classes in object-oriented programming language and is termed as a light-weighted class that supports composition but not inheritance.
For instance, an address having a street name, city, pin code, etc. these properties can be grouped into a single structure called an address as shown in an example below:
The type keyword is used in this example to introduce a new type. It has a name of the type which is also known as the address preceding with struct keyword which is used for illustrating that the structure is defined. Inside curly braces, the struct contains various fields, each having a type and a name.
Note:
the struct can also be made compact by combining various fields of the same type as shown below:
To define a structure, use the syntax given below:
var an Address
The above code snippet creates a variable of type Address which is by default kept to 0. In structure, zero means all the values are set to 0. This means that fields like name, city, or all the string values are set to empty which is “”, and integer values are set to 0.
The variables can also be initialized of struct type using the string literals given below:
Note:
- The field values should be passed in the same order in which they were declared in the struct.
- The Go language also supports the name: value syntax for initializing the struct. This allows for the initialization of only a specific subset of fields and all the uninitialized fields are set to 0.
This can be well understood from the example given below:
Golang Program to Show How to Declare and Define the Struct
Output:
Struct Pointers in Golang
In the Go language, a pointer is a variable used for storing the address of the memory in another variable. Pointers are also known as special variables and are used for storing data in a particular memory location. To use a pointer in a struct, & an operator can be used which is also known as an address operator.
Let's understand this better by taking an example:
Golang Program to Illustrate the Concept of the Pointer to Struct
Output:
Explanation:
In this example, a structure is created which is named Employee along with two variables. In the main method, an instance of struct is created i.e emp in which the address of the struct can be passed along with the pointer.
Accessing and Modifying Struct Element Using Struct Pointer
By using a pointer, we can access individual members of the struct. This can be better understood by taking an example.
Program to Access the Field of a Struct Using Pointer
Output:
Explanation:
In this example, a struct type pointer is used for accessing the struct members. The ptr.name pointer gives the value of the named member while the ptr.age pointer gives the value of the age member. Here to access the struct members using pointers, a dot operator is used.
Note: dereference operator (`*`) can also be used for accessing the members of the struct. For instance,
By using a struct pointer, values can also be modified.
This can be better understood from the example given below:
Golang Program to Illustrate the Concept of the Pointer to Struct
Output:
Explanation:
In this example, the name is changed using a pointer in a struct. The original value of the name is ABC along with the id 19078 but by using the pointer, the name is changed to XYZ keeping the id unchanged.
Benefits of Using Struct Pointer
Following are the benefits of using a pointer in the struct:
- It is a data type that stores the address of the memory location.
- Values can be manipulated by using the address of memory location efficiently.
- The original value is modified rather than making a copy of it and then modifying it.
- It makes it easy to change any value without duplicating it thereby saving memory.
Conclusion
- The struct keyword in golang, also known as "structure", is a user-defined data type that allows us to combine or group items of different types into a single type. Any real-world entity that has some set of properties and fields can be represented as a struct.
- The struct keyword in golang, also known as structure, is a user-defined data type that allows us to combine or group items of different types into a single type. It is typically compared with classes in object-oriented programming language and is termed as a light-weighted class that supports composition but not inheritance.
- To define a structure, use the syntax given below: var an Address
- The struct can also be made compact by combining various fields of the same type as shown below: