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!
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
And in similar fashion, ‘Product’ will store Product_ID, Product_Name, Price, Rating, Image wherever we create one ‘Product’.
When we type ‘var x User’, we have to insert the parameters.
As you can see, we have inserted 4321, “David”, “Malyan”, “tygrte%!ty”, “firstname.lastname@example.org, “5432167898”, “1ayt”, “yes”, “D4321” to define ‘User’
Similarly, we have inserted 786542, “Guitar”, 3200, 9, “yes” to define ‘Product’
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.
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.