So far, we have seen Data Types that are used to store an integer, a floating point number or a String.
But what if we need a Custom Data Type that could hold an integer value as well a String value.
Let us take a real example of, say a Student.
A Student can have a 'Name', a 'Roll Number' and 'Age'. Well, there can be other attributes but let's consider only three.
Now, the Student can be considered as a custom data type, that is going to hold 'Name', a 'Roll Number' and 'Age' of a Student
Now, if you think practically, let us say, there are two students.
i.e. A guy named 'John' whose roll number is 65 and age is 10 and a girl named 'Rakhi' who roll number is 50 and age is 8.
So, 'student' can be considered as a 'Structure'.
And, 'Student' is just a 'Structure' that is just a Blue print and doesn't exist physically.
And if you see the above diagram, 'student1' and 'student2' are custom type variables that exist physically (i.e. The John and Rakhi are the Students and they have physical existence).
Now, let us see, how can we implement 'Structures' in Go.
package main import "fmt" type student struct { name string roll int age int } func main() { var student1 student student1.name = "John" student1.roll = 65 student1.age = 10 fmt.Println(student1) fmt.Println(student1.name) fmt.Println(student1.roll) fmt.Println(student1.age) var student2 student student2.name = "Rakhi" student2.roll = 50 student2.age = 8 fmt.Println(student2) fmt.Println(student2.name) fmt.Println(student2.roll) fmt.Println(student2.age) }
So, in the above example, we have created a Structure named 'Student'.
type student struct { name string roll int age int }
The syntax of declaring a 'Structure' is quite simple.
And we have a 'Structure' named 'student'.
Now, just remember, a 'Structure' has no existence of its own. It only comes to effect once a variable is created out of it(i.e. 'student' type variable in this case).
And that is what we have done in the next line. We have created a student type variable out of 'student' Structure.
Creating a student type variable is just like creating a variable of 'int' or 'string' type.
So, the Structure type variable 'student1' is created.
And 'student1' variable is for the student named 'John' whose roll is '65' and age is '10'.
Now, it's time to assign the above values to the Structure type variable 'student1'. And we do it using the dot operator '.'.
And the Structure type variable 'student1' is created.
Then we create the Structure type variable 'student2'.
And 'student2' variable is for the girl named 'Rakhi' whose roll is '50' and age is '8'.
So, the Structure type variables, 'student1' and 'student2' of the Structure 'student' has the above values.
package main import "fmt" type student struct { name string roll int age int } func main() { student1 := student{name: "John", roll: 65, age: 10 } fmt.Println(student1) fmt.Println(student1.name) fmt.Println(student1.roll) fmt.Println(student1.age) student2 := student{name: "Rakhi", roll: 50, age: 8 } fmt.Println(student2) fmt.Println(student2.name) fmt.Println(student2.roll) fmt.Println(student2.age) }
Well! In the above code, we have used a different way to initialise the Structure variable using ':=' while declaration.
All we have done is, used the Structure name on the right hand side followed by braces '{}'.And all the values to be initialised are kept inside the braces '{}'.
But if you see a real Student. Other name, age and roll number, they have some behaviours,like they play, they speak e.t.c.
But we didn't find the behaviours implemented in the Structure.
Well! Go does it differently. We will be seeing next, how Go uses behaviours/functions for Structures.