Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Type Conversion in Go

Type conversion is the process in which we convert the data type of an expression to a required data type of our choice. In many languages, the compiler does it implicitly, which we sometimes call Type Coercion. But in Go, the compiler doesn't support implicit conversions, and if we need to convert one data type into another, we need to do it manually or explicitly.

An explicit conversion is an expression that generally looks like below:

The syntax for Type Conversion in Golang

Conversion = Type "(" Expression [ "," ] ")" .

In the above syntax, the Type identifier denotes the type that we want the final expression to have. The Expression identifier is the expression whose type we are converting. Finally, the Conversion identifier denotes the resultant value.

The expression T(v) converts the value v to the type T.

There are different things that we need to take care of when we are trying to convert one type to another.

If the type starts with the operator * or <-, then it is recommended to enclose the Type identifier in parentheses to avoid ambiguity.

Consider the cases shown below:

*Point(p)        // enclose *Point in parantheses
(*Point)(p)      // p is converted to *Point

<-chan int(c)    // enclose <-chan int in parantheses
(<-chan int)(c)  // c is converted to <-chan int

There are different conversions that can be done in Go. We will explore some of the most used cases of these conversions. These are:

  • int to float64 Conversion

  • float64 to int Conversion

  • int to unit Conversion

  • uint to int Conversion

  • string to []byte Conversion

  • []byte to string Conversion

  • string to []rune Conversion

  • []rune to string Conversion

Let's explore all the above-mentioned cases one by one.

int to float Conversion in Go

In order to convert an int value into a float value, we need to pass the int value as v to the expression T(v), where T will be of float data type.

Consider the example shown below:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var id int = 13
	fmt.Println("id is:", id)
	fmt.Println("id type is:", reflect.TypeOf(id))

	new_id := float64(id)
	fmt.Println("new_id is:", new_id)
	fmt.Println("new_id type is:", reflect.TypeOf(new_id))
}

In the above example, we have two variables, namely id, and new_id. The variable named id is of type int and we converted it into a variable of type float64.

Note: It should be noted that we can check the type of any expression or variable in Go with the help of the TypeOf function provided by the reflect package.


id is: 13
id type is: int
new_id is: 13
new_id type is: float64

float to int Conversion in Go

In order to convert a float value into an int value, we need to pass a float value as v to the expression T(v), where T will be of int data type.

Consider the example shown below:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var id float64 = 13.23
	fmt.Println("id is:", id)
	fmt.Println("id type is:", reflect.TypeOf(id))

	new_id := int(id)
	fmt.Println("new_id is:", new_id)
	fmt.Println("new_id type is:", reflect.TypeOf(new_id))
}

In the above example, we have two variables, namely id, and new_id. The variable named id is of type float64 and we converted it into a variable of type int.


id is: 13.23
id type is: float64
new_id is: 13
new_id type is: int

int to unit Conversion in Go

The difference between an int and uint is that int represents both the positive and negative numbers, whereas the uint represents the +ve numbers only. It is unsigned int in golang.

In order to convert an int value into a uint value, we need to pass the int value as v to the expression T(v), where T will be of uint data type.

Consider the example shown below:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var id int = 13
	fmt.Println("id is:", id)
	fmt.Println("id type is:", reflect.TypeOf(id))

	new_id := uint(id)
	fmt.Println("new_id is:", new_id)
	fmt.Println("new_id type is:", reflect.TypeOf(new_id))
}


id is: 13
id type is: int
new_id is: 13
new_id type is: uint

uint to int Conversion in Go

In order to convert a uint value into an int value, we need to pass the uint value as v to the expression T(v), where T will be of int data type.

Consider the example shown below:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var id uint = 13
	fmt.Println("id is:", id)
	fmt.Println("id type is:", reflect.TypeOf(id))

	new_id := int(id)
	fmt.Println("new_id is:", new_id)
	fmt.Println("new_id type is:", reflect.TypeOf(new_id))
}


id is: 13
id type is: uint
new_id is: 13
new_id type is: int

string to []byte Conversion in Go

We know that in Go, a string is basically a sequence of bytes, and we can convert between these two types in both directions, i.e from a string to a slice of byte and from a slice of byte to a string.

In the example shown below, we are converting a string into a slice of bytes.

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var name string = "studytonight"
	fmt.Println("name  is:", name)
	fmt.Println("name type is:", reflect.TypeOf(name))

	new_name := []byte(name)
	fmt.Println("new_name is:", new_name)
	fmt.Println("new_name type is:", reflect.TypeOf(new_name))
}


name is: studytonight
name type is: string
new_name is: [115 116 117 100 121 116 111 110 105 103 104 116]
new_name type is: []uint8

[]byte to string Conversion in Go

To convert a slice of bytes into a string, we just need to explicitly tell the compiler about the conversion and specify string type.

Consider the example shown below:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var bytesData []byte = []byte{70, 71, 72, 73}
	fmt.Println("bytesData  is:", bytesData)
	fmt.Println("bytesData type is:", reflect.TypeOf(bytesData))

	stringData := string(bytesData)
	fmt.Println("stringData is:", stringData)
	fmt.Println("stringData type is:", reflect.TypeOf(stringData))
}


bytesData is: [70 71 72 73]
bytesData type is: []uint8
stringData is: FGHI
stringData type is: string

string to []rune Conversion in Go

A rune in Go is a single character and is usually denoted with the help of the ''. In order to convert a string to a slice of rune, we just need to explicitly do the type conversion, and tell the compiler about the conversion.

Consider the example shown below:

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var name string = "studytonight"
	fmt.Println("name  is:", name)
	fmt.Println("name type is:", reflect.TypeOf(name))

	runeSlice := []rune(name)
	fmt.Println("runeSlice is:", runeSlice)
	fmt.Println("runeSlice type is:", reflect.TypeOf(runeSlice))
}


name is: studytonight
name type is: string
runeSlice is: [115 116 117 100 121 116 111 110 105 103 104 116]
runeSlice type is: []int32

[]rune to string Conversion in Go

Consider the example shown below which explores the case where we want to convert a slice of runes into a string.

package main

import (
	"fmt"
	"reflect"
)

func main() {
	var runeData []rune = []rune{'\u0041', '\u0042', '\u0043', '\u20AC'}
	fmt.Println("runeData  is:", runeData)
	fmt.Println("runeData type is:", reflect.TypeOf(runeData))

	stringData := string(runeData)
	fmt.Println("stringData is:", stringData)
	fmt.Println("stringData type is:", reflect.TypeOf(stringData))
}


runeData is: [65 66 67 8364]
runeData type is: []int32
stringData is: ABC€
stringData type is: string

Conclusion

In the above article, we learned about how to do different types of type conversions in Go. We also learned that in Go, there's no such thing as type coercion(implicit conversion), and if we want to convert one data type into another we need to do it explicitly.