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




GO - NUMBERS


As we have seen in the 'Data Types' topic, there are three types of Numbers supported by Go. Integers, floating point numbers and complexs numbers.

  1. 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 :


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


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


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


    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 :



        -22337203685477580
  2. 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
  3. 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)