Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




GO - STRUCTURE


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.


java_Collections

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.


java_Collections

So, 'student' can be considered as a 'Structure'.


java_Collections

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.


Example :



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) 
}  


Output :



 {John 65 10}
 John
 65
 10
 {Rakhi 50 8}
 Rakhi
 50
 8

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.

  1. It begins with a 'type' keyword.
  2. Followed by the 'Structure' name'(i.e. 'student' in this case).
  3. Followed by the 'struct' keyword.
  4. And the structure block should be inside braces '{}'.

java_Collections

And we have a 'Structure' named 'student'.


java_Collections

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.


var student1 student

Creating a student type variable is just like creating a variable of 'int' or 'string' type.


java_Collections

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 '.'.


student1.name = "John"
student1.roll = 65
student1.age = 10

And the Structure type variable 'student1' is created.


java_Collections

Then we create the Structure type variable 'student2'.


var student2 student

And 'student2' variable is for the girl named 'Rakhi' whose roll is '50' and age is '8'.


student2.name = "Rakhi"
student2.roll = 50
student2.age = 8

java_Collections

So, the Structure type variables, 'student1' and 'student2' of the Structure 'student' has the above values.


Other way to define a Structure type variable


Example :



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) 
     
}   


Output :



 {John 65 10}
 John
 65
 10
 {Rakhi 50 8}
 Rakhi
 50
 8

Well! In the above code, we have used a different way to initialise the Structure variable using ':=' while declaration.


student1 := student{name: "John", roll: 65, age: 10 }

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 '{}'.


java_Collections

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.