Golang Data Types and Operations

Golang data types and operations

In this blog, we will take a look at the Golang primitive Data types and Operations or The Go Programming language.

Before starting Golang data types learn about golang variables tutorial.

Golang Boolean Data Type

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

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 default value which is false.

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.

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

TypeRange
int8-128 to 127
int16-32768 to 32767
int32-231 to 231 -1
int64-263 to 263 -1
Type and Range of Signed integer in Golang

Golang Unsigned Integer Data type

TypeRange
uint80 to 255
uint160 to 65535
uint320 to 232 -1
Type and Range of unsigned integer in Golang

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

TypeSize
float32(+-) 1.18E-38 to (+-) 3.4E38
float64(+-) 2.23E-308 to (+-) 1.8E308
Type and Range of float types in Golang
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

Golang Complex numbers

There are two size types of Golang complex data types:

  1. complex64
  2. complex128
package main

import (
	"fmt"
)

func main() {

	var n complex64 = 1 + 5i
	var m complex128 = 2 + 4i
	var a complex64 = 0i
	b := 4 + 2i
	fmt.Printf("%v,%T\n", n, n)
	fmt.Printf("%v,%T\n", m, m)
	fmt.Printf("%v,%T\n", a, a)
	fmt.Printf("%v,%T\n", b, b)

}

Output:-

(1+5i),complex64
(2+4i),complex128
(0+0i),complex64
(4+2i),complex128

Golang Real & Imaginary functions

package main

import (
	"fmt"
)

func main() {

	var n complex64 = 1 + 5i
	fmt.Printf("%v,%T\n", n, n)
	fmt.Printf("%v,%T\n", real(n), real(n))
	fmt.Printf("%v,%T\n", imag(n), imag(n))

}

Output:

1,float32
5,float32

For complex64 type, each real and imaginary value has float32 type and for complex128 each real and imaginary value has float64 type.

Golang Complex data types Operations

package main

import (
	"fmt"
)

func main() {

	var n complex64 = 1 + 5i
	var m complex64 = 2 + 3i
	fmt.Printf("%v,%T\n", n+m, n+m)
	fmt.Printf("%v,%T\n", n-m, n-m)
	fmt.Printf("%v,%T\n", n*m, n*m)
	fmt.Printf("%v,%T\n", n/m, n/m)

}

Output:

(3+8i),complex64
(-1+2i),complex64
(-13+13i),complex64
(1.3076923+0.53846157i),complex64

Golang Integer Data Type Alias

Bytes and Rune are also types of golang that are alias for integer type in Golang.

TypeAlias for integer type
byteuint8
runeint32
Type and Alias for integer type in Golang

Golang String Data Type

package main

import (
	"fmt"
)

func main() {

	s := "This is a String"
	fmt.Printf("%v,%T\n", s, s)
        fmt.Printf("%v,%T\n", s[3], s[3])
	fmt.Printf("%v,%T\n", string(s[3]), s)

}

Output:

This is a String,string
115,uint8
s,string

Golang String data types are immutable so we cannot do something like this:

s[3] = ‘a’

But, we can treat strings like an array and can look at any index value.

fmt.Printf(“%v,%T\n”, s[3], s[3])

s[3] returns 115 which is of uint8 type because each character is of byte type which is an alias for Golang uint8 data types. To get the correct string in the output we will have to convert it into the string using string type conversion.

Golang String to Array of byte

package main

import (
	"fmt"
)

func main() {

	s := "This is a String"
	b := []byte(s)
	fmt.Printf("%v,%T\n", s, s)
	fmt.Printf("%v,%T\n", b, b)

}

Output:

s,string
[84 104 105 115 32 105 115 32 97 32 83 116 114 105 110 103],[]uint8

Golang String to array of rune

package main

import (
	"fmt"
)

func main() {

	s := "This is a String"
	b := []rune(s)
	fmt.Printf("%v,%T\n", s, s)
	fmt.Printf("%v,%T\n", b, b)

}

Output:

s,string
[84 104 105 115 32 105 115 32 97 32 83 116 114 105 110 103],[]int32

Golang Rune Data Type

package main

import (
	"fmt"
)

func main() {

	var r rune = 'a'

	fmt.Printf("%v,%T\n", r, r)

}

Output:

97,int32

The rune in Golang is an alias for the int32 data type. In simple terms, it behaves like a char type of other languages.

Golang Arithmetic Operations on Data types

package main

import (
	"fmt"
)


func main() {
	a := 5
	b := 10
	fmt.Printf("%v,%T\n", a, a)
	fmt.Printf("%v,%T\n", b, b)
	fmt.Printf("%v,%T\n", a+b, a+b)
	fmt.Printf("%v,%T\n", a-b, a-b)
	fmt.Printf("%v,%T\n", a*b, a*b)
	fmt.Printf("%v,%T\n", a/b, a/b)
	fmt.Printf("%v,%T\n", a%b, a%b)
}

Output:

5,int
10,int
15,int
-5,int
50,int
0,int
5,int

Golang Arithmetic operation on different size types variables is not possible

package main

import (
	"fmt"
)


func main() {
	var a int = 10
	var b int8 = 5
	fmt.Printf("%v,%T\n", a, a)
	fmt.Printf("%v,%T\n", b, b)
	fmt.Printf("%v,%T\n", a+b, a+b)

}

ERROR: – invalid operation: a + b (mismatched types int and int8)

Golang Type Cast

package main

import (
	"fmt"
)

func main() {
	var a int = 10
	var b int8 = 5
	fmt.Printf("%v,%T\n", a, a)
	fmt.Printf("%v,%T\n", b, b)
	fmt.Printf("%v,%T\n", a+int(b), a+int(b))

}

Output:-

10,int
5,int8
15,int

Golang Bitwise Operators

package main

import (
	"fmt"
)

func main() {
	a := 10
	b := 5

	fmt.Printf("%v,%T\n", a, a)
	fmt.Printf("%v,%T\n", b, b)
	fmt.Printf("%v,%T\n", a&b, a&b)
	fmt.Printf("%v,%T\n", a|b, a|b)
	fmt.Printf("%v,%T\n", a^b, a^b)
	fmt.Printf("%v,%T\n", a&^b, a&^b)
}

Output:

10,int
5,int
0,int
15,int
15,int
10,int

Also read about Future of Server-side language?

Learn More about Golang from the documentation of golang.

Golang Primitive Data types

1. bool
2. signed integers
3. unsigned integers
4. float types
5. string type
6. complex types
7. byte
8. rune

Golang Integer data type alias

rune – alias for int32
byte – alias for uint8

Golang Converting a string to an array of byte/rune

s := “this is a string”
b := []byte(s)
r := []rune(s)
fmt.Printf(“%v,%T\n”, b, b)
fmt.Printf(“%v,%T\n”, r, r)

Output:
b = [84 104 105 115 32 105 115 32 97 32 83 116 114 105 110 103],[]uint8
r = [84 104 105 115 32 105 115 32 97 32 83 116 114 105 110 103],[]int32

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *