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




GO - DATA TYPES


We have seen a variable is used to hold a value. Be it a number or a name or even a sentence.


i.e.


var x = 5

Or


var y = "Hello World"

But how will Go come to know, if the value is a number or a name?


Well! Once again, that's the beauty of Go. Go makes an intelligent guess, if the value is a number or a name or a sentence or something else and create the variable accordingly.


But! Just think for a moment. Go must be using some mechanism by which it can determine if the value assigned to a variable is a number or a name or a sentence or something else.


And thus 'Data Types' comes into picture.


Data Types


'Data Types' are the hidden gem that is used by Go to determine if the variable is going to hold a number or a name.


But why does Go needs to determine if the variable is going to hold a number or a name?
Can it not create one type of variable and store all the values there.


It cannot do that. Because if you want to store a number. It would take less space.


java_Collections

On the other hand, if you want to store a String like 'Hello World'. It would take a larger space.


java_Collections

So, whenever you want to create assign a String to a variable.


var y = "Hello World"

Go makes an intelligent guess and finds out, it is a String type value and creates the storage space accordingly.


java_Collections

The above 'Data Type' for String is called 'str' 'Data Type'.


And similarly for a number, for a decimal value e.t.c. Go has different 'Data Types'.


Note : We will keep the explanation of all the Data Types as short as possible. And will be having separate tutorials for all the Data Types.

Array of all Data Types used in Go

  1. String

    As we have seen in the above example, a String is a set of letters/alphabets.

    The 'Data Type' for String is 'string'.

    When Go finds data inside Double Quotes (""). It creates a String type variable for that value.

    var y = "Hello World"

    So, the 'Data Type' of a variable is determined by Go and is hidden from us. Now, if we want to specify the 'Data Type' of a variable, so that it only holds string type value.

    Example :



    package main
    import "fmt"
     
    func main() {
         
        var x string = "Hello World";
        fmt.Println(x);
    }   
    


    Output :



      Hello World


    In the above code we have initialised the variable 'y' with the String 'Hello World'.

    var x string = "Hello World";

    Now, the 'string' keyword after the variable 'x' will make sure that no values other than String would be allowed in it.

  2. Integers

    Integers are the whole numbers (i.e. 7 or 5).

    Any whole number(Be it '-4' or '45') can be represented by the 'int' datatype

    Let us see it below :

    1. int Data Type

      The 'Data Type' for a whole number is 'int'.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x int = 5
          fmt.Println(x) 
      }
      


      Output :



        5


    Although the 'int' data type is capable of storing all types of numbers, i.e. A very large number or a small number or a negative number.

    But don't you think, it would be a lot better, if we could have different data types for different types of numbers.

    And luckily, Go has that.

    Let us see them below :

    1. uint8 Data Type

      The 'uint8' Data Type represents, unsigned 8 bit integers. i.e. It would store numbers from 0 to 255.

      Now, if you break 'uint8' datatype,

      'u' - Unsigned, i.e. No negative numbers would be allowed.

      'int' - It would hold an integer.

      '8' - Stores an '8' bit number.

      So, if you combine all the three (i.e. 'u', 'int' & '8'), it is a datatype, 'uint8' that stores an unsigned 8 bit integer.

      Note : For now you don't have to understand, what a 8 bit number is. Just remember the range(i.e. 0 to 255) and that would be enough.


      Anything values other than 0 to 255 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x uint8 = 35
          fmt.Println(x) 
      }	
      


      Output :



        35


      So, in the above example, we have created a variable 'x' of type 'uint8'. And assigned the value '35' to it.

      And since '35' falls in the range of 0 to 255, '35' is inserted to x.

      Now, what if, we try to insert a value that is other than the range of 0 to 255. Say, 456.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x uint8 = 456
          fmt.Println(x) 
      }	
      


      Output :



        constant 456 overflows uint8


      And we get the error message that 'constant 456 overflows uint8'.

      This actually means that the value '456' doesn't belongs in the range of 0 to 255. And cannot be inserted to the variable 'x'.

    2. uint16 Data Type

      The 'uint16' Data Type represents, unsigned 16 bit integers. i.e. It would store numbers from 0 to 65535.

      Anything values other than 0 to 65535 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x uint16 = 54364
          fmt.Println(x) 
      }	
      


      Output :



        54364


      So, in the above example, we have created a variable 'x' of type 'uint16'. And assigned the value '54364' to it.

      And since '54364' falls in the range of 0 to 65535, '54364' is inserted to x.

    3. uint32 Data Type

      The 'uint32' Data Type represents, unsigned 32 bit integers. i.e. It would store numbers from 0 to 4294967295.

      Anything values other than 0 to 4294967295 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x uint32 = 645738765
          fmt.Println(x) 
      }
      


      Output :



        645738765

    4. uint64 Data Type

      The 'uint64' Data Type represents, unsigned 64 bit integers. i.e. It would store numbers from 0 to 18446744073709551615.

      Anything values other than 0 to 4294967295 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x uint64 = 8765432876509876567
          fmt.Println(x) 
      }
      


      Output :



        8765432876509876567

    5. uint64 Data Type

      The 'uint64' Data Type represents, unsigned 64 bit integers. i.e. It would store numbers from 0 to 18446744073709551615.

      Anything values other than 0 to 4294967295 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x uint64 = 8765432876509876567
          fmt.Println(x) 
      }
      


      Output :



        8765432876509876567

    6. int8 Data Type

      The 'int8' Data Type represents, signed 8 bit integers. i.e. It would store numbers from -128 to 127.

      Since, it is a signed data type, it can store negative numbers as well.

      Anything values other than -128 to 127 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x int8 = -125
          fmt.Println(x) 
      }	
      


      Output :



        -125


      So, in the above example, we have created a variable 'x' of type 'int8'. And assigned the value '-125' to it.

      And since '-125' falls in the range of -128 to 127, '-125' is inserted to x.

    7. int16 Data Type

      The 'int16' Data Type represents, signed 16 bit integers. i.e. It would store numbers from -32768 to 32767.

      Anything values other than -32768 to 32767 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x int16 = -24364
          fmt.Println(x) 
      }	
      


      Output :



        -24364

    8. int32 Data Type

      The 'int32' Data Type represents, signed 32 bit integers. i.e. It would store numbers from -2147483648 to 2147483647.

      Anything values other than -2147483648 to 2147483647 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x int32 = -243435664
          fmt.Println(x) 
      }	
      


      Output :



        -243435664

    9. int64 Data Type

      The 'int64' Data Type represents, signed 32 bit integers. i.e. It would store numbers from -9223372036854775808 to 9223372036854775807.

      Anything values other than -9223372036854775808 to 9223372036854775807 would result in an error.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x int64 = -223372036854775808
          fmt.Println(x) 
      }	
      


      Output :



        -223372036854775808

  3. float Data Type

    There are two types of 'Data Type' for a floating point number.

    They are :

    1. float32 Data Type

      The 'float32' Data Type represents, 32 bit floating point number.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x float32 = 5.987
          fmt.Println(x) 
      }	
      


      Output :



        5.987


      So, in the above example, we have created a variable 'x' of type 'float32'. And assigned the value '5.987' to it.

    2. float64 Data Type

      The 'float64' Data Type represents, 64 bit floating point number. In simple words, it stores a floating point number which is a little large.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x float64 = 5.9875532556
          fmt.Println(x) 
      }	
      


      Output :



        5.9875532556

  4. complex Data Type

    In Mathematics, you can find lots of formulas where there is a real and an imaginary part.

    Say, for example

    x = 2 + 5i


    Where 'i' is the imaginary part.

    To represent the complex numbers, there are two types of 'Data Type' for a complex numbers.

    They are :

    1. complex64 Data Type

      It supports Complex numbers with float32 as real and imaginary parts.

      Example :



      package main
      import "fmt"
      
      func main() {
                
          var x complex64 = complex(2, 5)
          fmt.Println(x) 
      }	
      


      Output :



        (2+5i)


      So, the expression,

      var x complex64 = complex(2, 5)


      Converts the number '2' and '5' to a complex expression.

      (2+5i)


    2. complex128 Data Type

      It supports Complex numbers with floa64 as real and imaginary parts.

      Example :



      package main
      import "fmt"
         
      func main() {
                    
          var x complex128 = complex(6, 8)
          fmt.Println(x) 
      }
          


      Output :



        (6+8i)


      So, the expression,

      var x complex128 = complex(6, 8)


      Converts the number '6' and '8' to a complex expression.

      (6+8i)

  5. Booleans

    Boolean represents just two values, i.e. 'true' or 'false'.

    1. bool Data Type

      A 'bool' 'Data Type' can accept only 'true' or 'false'.

      Example :



      package main
      import "fmt"
      
      func main() {
             
          var x bool = true
          var y bool = false
          fmt.Println(x) 
          fmt.Println(y) 
      }
      


      Output :



        true
        false