Please note, this is a STATIC archive of website www.simplilearn.com from 27 Mar 2023, cach3.com does not collect or store any user information, there is no "phishing" involved.

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.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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 from the Best in the Industry!

Caltech PGP Full Stack DevelopmentExplore Program
Learn from the Best in the Industry!

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!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.