Go has three basic data types:
- Bool
- Numerical
- String
- 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 :
- Boolean data type: datatype is declared with the bool keyword and can take the values true or false.
- 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 :
- This program shows some different ways to declare Boolean variables:
-
-
- Integer data type: are used to store a whole no. without decimals, like 35.50.
-
-
-
- Integer has two types:
- Signed integers
- 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 :
- Integer has two types:
-
- 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 :
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 :
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) }
output :
3)String Data type: the string datatype is use to store a sequence of characters. String values must be surrounded 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:
The post Go Data Type appeared first on Prwatech.
This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here