loading...

Golang Data Types and Operations

#go
divshekhar profile image Divyanshu Shekhar ・2 min read

Golang Boolean Data Type

Go Boolean data types are used to store either true or false value.

Boolean Data type

var n bool = true
fmt.Printf("%v, %T\n", n, n)
Output:

true, bool

If we don’t initialize the bool data type variable then it takes 0 as the default value which is false.

Default Bool Value

var n bool
fmt.Printf("%v, %T\n", n, n)
Output:

false, bool

Bool data type variables are used for condition check and as flags.

Bool Condition Check

package main

import (
         "fmt"
)

func main() {
    n := 1 == 1
    m := 1 == 2
    fmt.Printf("%v, %T\n", n, n)
    fmt.Printf("%v, %T\n", m, m)
}
Output:

true, bool

false, bool

Golang Int Size

Golang Signed integer Data type

Type Range
int8 -128 to 127
int16 -32768 to 32767
int32 -231 to 231 -1
int64 -263 to 263 -1

Type and Range of Unsigned integer in Golang

Type Range
uint8 0 to 255
uint16 0 to 65535
uint32 0 to 232 -1

If you want to store a very long positive number, the Golang unsigned integer data types will do the work for you.

In many of the languages, there is a case of integer overflow but you can handle this error in Golang.

Golang Float Size

Type Size
float32 (+-) 1.18E-38 to (+-) 3.4E38
float64 (+-) 2.23E-308 to (+-) 1.8E308
Type and Range of float types in Golang

Golang Float Data types

package main

import (
    "fmt"
)

func main() {
    n := 3.14
    m := 13.7e72
    j := 2.1E14
    fmt.Printf("%v,%T\n", n, n)
    fmt.Printf("%v,%T\n", m, m)
    fmt.Printf("%v,%T\n", j, j)
}
Output:-

3.14,float64
1.37e+73,float64
2.1e+14,float64

Learn about more Golang Data types and Operations from the original Post.

Posted on by:

Discussion

pic
Editor guide