What Are Structs in Golang?

What Are Structs in Golang?'s picture

Yes, you got that part right. Structs are short for structures. In this blog, we will understand the role of structs in Golang. In simple words, the idea behind structure is to keep things together. Golang helps us to create types that have their own custom behavior. Now why would we need that? Well, sometimes the current types that are provided to us are not sufficient. Go Developers wish to combine and use the types to create their own type.

So, let us get started!

Creating First Struct

  • If you refer to the program below, you will see that we have created a ‘type User struct’ and ‘type Product struct’. Here, the struct defines that it is a custom type.
  • Now, we have to list the fields that we wish to be a part of ‘User’ struct and ‘Product’ struct. Let us assume that we take nine fields in the ‘User’ struct, namely

ID uint64

First_name string

Last_name string

Password string

Email string

Phone string

Token string

Refresh_Token string

User_ID string

So, ‘User’ will store ID, First_name, Last_name, Password, Email, Phone, Token, Refresh_Token, User_ID values whenever we create a single ‘User’

Similarly, we include the fields in the ‘Product’ struct

Product_ID uint64

Product_Name string

Price uint64

Rating uint8

Image string

And in similar fashion, ‘Product’ will store Product_ID, Product_Name, Price, Rating, Image wherever we create one ‘Product’.

Creating First Struct
  • Now, in the function main (), we have to define what makes up

When we type ‘var x User’, we have to insert the parameters.

As you can see, we have inserted 4321, “David”, “Malyan”, “tygrte%!ty”, “abc@gmail.com, “5432167898”, “1ayt”, “yes”, “D4321” to define ‘User’

Similarly, we have inserted 786542, “Guitar”, 3200, 9, “yes” to define ‘Product’

  • Finally, we print out x.Phone and y.Product_ID, which should give us results 5432167898 and 786542
  • As we run the program in the terminal, we get to see ‘5432167898’ and ‘786542’ as the result. So, the struct has been successfully created.

Alternative Way to Define Custom Type

  • There is another way to define User and Product (the process known as literal). For that, you should take a look at the program below.
Alternative Way to Define Custom Type

  • In order to assign values in x, we write it as x := User {ID: 4321, First_Name: “David, Last_Name: “Malyan”, Password: “tygrte%!ty, Email: “abc@gmail.com”, Phone: “5432167898”}

Here, we provide only value for every element in x except for Token, Refresh_Token, User_ID. In the case of y, we provide values for Product_ID, Product_Name, Price. Since, we do not assign any value for int uint8 type Rating, it should ideally display default value, i.e, 0, in place of Rating, during runtime.

  • So, we run the program in the terminal and the output is as expected.

program in the terminal

Hopefully, you have understood the concept. In order to get a grasp of it, perform coding on VS Code or any other IDE and see the result for yourself.

Build Your Golang Team