Welcome to Golang Programs, Get the best new programs in your inbox, daily.

Integer Float String Boolean

Go provides both signed and unsigned integer arithmetic. There are four distinct sizes of signed integers—8, 16, 32, and 64. Bits represented by the types int8, int16, int32, and int64 and corresponding unsigned versions uint8, uint16, uint32, and uint64.

package main
import "fmt"
func main(){
    var n1 uint8 // Unsigned 8-bit integers (0 to 255)
    n1 = 200
    fmt.Println(n1)
    var n2 uint16 // Unsigned 16-bit integers (0 to 65535)
    n2 = 54200
    fmt.Println(n2)
    var n3 uint32 // Unsigned 32-bit integers (0 to 4294967295)
    n3 = 98765214
    fmt.Println(n3)
    var n4 uint64 // Unsigned 64-bit integers (0 to 18446744073709551615)
    n4 = 1844674073709551615
    fmt.Println(n4)
    var n5 int8 //Signed 8-bit integers (-128 to 127)
    n5 = -52
    fmt.Println(n5)
    fmt.Println(n5*-1)
    var n6 int16 // Signed 16-bit integers (-32768 to 32767)
    n6 = -32552
    fmt.Println(n6)
    fmt.Println(n6*-1)
    var n7 int32 // Signed 32-bit integers (-2147483648 to 2147483647)
    n7 = -98658754
    fmt.Println(n7)
    fmt.Println(n7*-1)
    var n8 int64 // Signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
    n8 = -92211111111111111
    fmt.Println(n8)
    fmt.Println(n8*-1)
}
Output 
200   
54200          
98765214         
1844674073709551615         
-52                                                                                                                                                      
52   
-32552         
32552  
-98658754
98658754
-92211111111111111
92211111111111111

Go provides two sizes of floating-point numbers, float32 and float64. Their arithmetic properties are governed by the IEEE 754 standard implemented by all modern CPUs. Go provides two sizes of complex numbers, complex64 and complex128, whose components are float32 and float64 respectively.

package main
import "fmt"
func main(){
    var f1 float32 = 1677.7216 // IEEE-754 32-bit floating-point numbers
    fmt.Println(f1)
    var f2 float64 = 18787677.878716 // IEEE-754 64-bit floating-point numbers
    fmt.Println(f2)
    var f3 complex64 = 8789579877.721 // Complex numbers with float32 real and imaginary parts
    fmt.Println(f3)
    var f4 complex128 = 985214745.7216 // Complex numbers with float64 real and imaginary parts
    fmt.Println(f4)
}
Output 
1677.7216
1.8787677878716e+07                                                                                                                                     
(8.78958e+09+0i) 
(9.852147457216e+08+0i)   

A boolean value (named after George Boole) is a special 1-bit integer type used to represent true and false (or on and off).A boolean type represents the set of boolean truth values denoted by the predeclared constants true and false. The boolean type is bool.

Strings in Go are a sequence of UTF-8 characters enclosed in double quotes (") "Hello, World" or backticks `Hello, World`. If you use the single quote (') you mean one character (encoded in UTF-8) — which is not a string in Go. Once assigned to a variable the string can not be changed: strings in Go are immutable.A space is also considered a character.

package main
import "fmt"
func main(){
    var a,b bool // Boolean type declaration
    a=true
    b=false
    fmt.Println(a,b)
    var city = "Washington" // " is necessary for string assignmnet
    fmt.Println(city)
    var country string 
    country = "Germany"
    fmt.Println(country)
}
Output 
true false
Washington
Germany