Go does not follow traditional Object-Oriented Programming. Rather, Go possesses structures that hold a combination of data types. Go does not have class-object architecture as well. In this tutorial, you will learn about the struct type in Go programming with the help of examples.
What Is Struct?
In Go programming, a structure or struct is a user-defined type to store a collection of different fields into a single field.
For example, suppose you have a player and want to store his name and age. You can create two variables, name, and age, to store the values. Now, suppose you have a football team and want to store the same information for each player.
This is where struct comes into play. You can design a struct that stores the values of the name and age of each player.
Declaring a Struct
In the above snippet, the type keyword introduces a new type, followed by the name of the type (Player) and the keyword struct. The struct contains a collection of various fields inside the curly braces. Each field possesses a name and a type.
Note: You can make the snippet compact by combining the various fields of the same type, as shown in the below example:
Let's understand the declaration of the struct with the help of a program.
Creating Instances of Struct Types
To work with a struct, you need to create an instance of it. The var keyword initializes a variable and, using dot notation, values are assigned to the struct fields.
You must now create a struct type Player.
Here, you have assigned the values to the struct while creating an instance ply1.
You will specify the value for each field name. The order of the fields can vary. It need not be the same as the order of the field names declared in the struct type.
Secondly, you will define an instance ply2 and assign value to struct variables
In ply2 struct, you have omitted the field names. In this case, it becomes necessary to maintain the order of fields as specified during the struct declaration.
The above program gives the following output:
Accessing Fields of Struct in Golang
You can access the individual fields of a struct using the struct instances.
ply1.name in the above program accesses the name field of the ply1 struct.
ply1.city accesses the city field of the ply1 struct.
ply1.age accesses the age field of the ply1 struct.
You have also modified the age of the player.
This program prints as follows:
You have seen through the examples that it would become quite tedious to keep using built-in data types. Therefore, you use the user-defined struct type to store complex data structures.
If you aim to build a software development career, you can check the Post-Graduate Program in Full Stack Development by Simplilearn. It can be the ideal solution to help you develop your career in the right direction.
In case you have queries or would like to provide your input to our editorial team regarding this “The Supreme Guide to Go Struct” tutorial, feel free to use the comments section below. Our team of SMEs will get back to you soon!