Go Data Type

  • date 17th August, 2022 |
  • by Prwatech |
  • 0 Comments

Understanding Data Types in Go

  Go has three basic data types:
  1. Bool
  2. Numerical
  3. String
 
  1. This program shows some of the different data types in Go
    package main
    import "fmt"
    
    func main() {
    	var a bool = true          // Boolean
    	var b int = 5              // Integer
    	var c float32 = 6.14       // Floating point number
    	var d string = "PRWATECH!" // String
    
    	fmt.Println("Boolean: ", a)
    	fmt.Println("Integer: ", b)
    	fmt.Println("Float:   ", c)
    	fmt.Println("String:  ", d)
    }	
    
    Output :
    PS C:\GO_Language\datatype> go run type.go
    Boolean:  true
    Integer:  5
    Float:    6.14
    String:   PRWATECH!
    
  2. Boolean data type: datatype is declared with the bool keyword and can take the values true or false.
    1. This program shows some different ways to declare Boolean variables:
      package main
      import "fmt"
      func main() {
          var b1 bool = true
          var b2 = true
          var b3 bool
          b4 := true
          fmt.Println(b1)
          fmt.Println(b2)
          fmt.Println(b3)
          fmt.Println(b4)
      }
      
      Output :
      PS C:\GO_Language\datatype> go run bool.go
      true
      true
      false
      true
      
      • Integer data type: are used to store a whole no. without decimals, like 35.50.
      • Integer has two types:
        1. Signed integers
        2. Unsigned integers
         
        • Singed integer:
        Signed integers, declared with one of the int keywords, can store both positive and negative values
        package main
        
        import "fmt"
        
        func main() {
            var x int = 50
            var y int = 40
            fmt.Printf("Type: %T, value: %v \n", x, x)
            fmt.Printf("Type: %T, value: %v", y, y)
        }
        
        Output :
        PS C:\GO_Language\datatype> go run int1.go
        Type: int, value: 50 
        Type: int, value: 40
        
  •          Unsigned integers:
Unsigned integers, declared with one of the uint keywords, can only store non-negative values:
package main
import "fmt"

func main() {
    var x uint = 50
    var y uint = 450

    fmt.Printf("Type: %T, value: %v \n", x, x)
    fmt.Printf("Type: %T, value: %v", y, y)
}

Output :
PS C:\GO_Language\datatype> go run int2.go
  
Type: uint, value: 50 
Type: uint, value: 450
  3)float data type:  are used to store positive and negative no. which a decimal point, like 35.3, -32, to 3423.243525.     Type                           size                              Range Float32                        32bits                          -34e+38 to 3.4e+38. Float64                        64bits                          -1.7e+308 to 3.4e+308. This program shows how to declare some variables of type float32:
package main

import "fmt"

func main() {
    var x float32 = 23.78
    var y float32 = 3.4e+38

    fmt.Printf("Type: %T, value: %v\n", x, x)
    fmt.Printf("Type: %T, value: %v", y, y)
}
output :
PS C:\GO_Language\datatype> go run float1.go
  
Type: float32, value: 23.78
Type: float32, value: 3.4e+38
    This Progeam shows how to declare a variable of type float64:
package main
import "fmt"

func main() {
    var x float64 = 1.7e+308
    
    fmt.Printf("Type: %T, value: %v", x, x)
    
}
check output :
PS C:\GO_Language\datatype> go run float2.go
   
Type: float64, value: 1.7e+308
    3)String Data type: the string datatype is use to store a sequence of characters. String values must be surround by double quotes:
package main

import "fmt"

func main() {
    var txt1 string = "parwatech"
    var txt2 string
    txt3 := "World 1"

    fmt.Printf("Type: %T, value: %\n", txt1, txt1)
    fmt.Printf("Type: %T, value: %v\n", txt2, txt2)
    fmt.Printf("Type: %T, value: %v\n", txt3, txt3)
}
output:
PS C:\GO_Language\datatype> go run str.go   
 
Type: string, value: parwatech 
Type: string, value:  
Type: string, value: World 1
Understanding Data Types in Go

Quick Support

image image