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.

Here's How to Land a Top Software Developer Job

Full Stack Developer - MERN StackExplore Program
Here's How to Land a Top Software Developer Job

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.

Go_Struct_1.

Note: You can make the snippet compact by combining the various fields of the same type, as shown in the below example:

Go_Struct_2.

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. 

Go_Struct_3

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:

Go_Struct_4

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Accessing Fields of Struct in Golang

You can access the individual fields of a struct using the struct instances.

Go_Struct_5

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:

Go_Struct_6.

Conclusion

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!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449