Type-casting in Golang

Sometimes we need to convert one data-type to another. In this post, we will go into detail about type-casting in the Go programming language.

Types in Go

There are many data-types in Go. The numbers, floating points, boolean and string.

  • Number: int, int32, int64, uint32, uint64 etc
  • Floating points: float32, float64, complex64, complex128
  • Boolean: bool
  • string: string

What is type-casting?

Type-casting means converting one type to another. Any type can be converted to another type but that does not guarantees that the value will remain intact or in fact preserved at all as we will see in this post.

Why is it important?

Type-casting is an important concept in general programming. It converts one type to another and whenever we need some other types for the expression type casting helps.

Type-casting syntax

The syntax for general type casting is pretty simple. just use that other type name as a function to convert that value.

v := typeName(otherTypeValue)

e.g. i := int(32.987) // casting to integer

Implicit type conversions

Unlike other languages, Go doesn’t support implicit type conversion. Although when dividing numbers, implicit conversions happen depending on the scenario. So we need to be very careful about what type to use where.

Basic Type-casting

Anywhere we need to change the type of the variable, the typecasting is needed. Sometimes typecasting may not be direct as discussed before. Here is an example of direct type conversion.

package main

import (
	"fmt"
)

func main() {
	var a int = 42
	f := float64(a)
	fmt.Println(f)       // 42
}

Conversions between string and int

There is a package called strconv which can be used to convert between string and int values. Below is the code on how to do that.

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var s string = "42"
	v, _ := strconv.Atoi(s)       // convert string to int
	
	fmt.Println(v)    // 42
	
	var i int = 42
	str := strconv.Itoa(i)        // convert int to string
	
	fmt.Println(str) // 42
}

Conversions between int and float

When conversion between int and floats happen the numbers may lose precision. Here are type conversions between int and floats.

package main

import (
	"fmt"
)

func main() {
	f := 12.34567
	i := int(f)  // loses precision
	fmt.Println(i)      // 12
	
	ii := 34
	ff := float64(ii)
	
	fmt.Println(ff)     // 34
}

As you can see in the code above, the conversion of a float to an int always loses precision.

Strings and bytes conversion

Strings are slices of bytes. So both can be converted to each other with minimal effort. Here is the simplest way to do that.

package main

import (
	"fmt"
)

func main() {
	var s string = "Hello World"
	var b []byte = []byte(s)     // convert ty bytes
	
	fmt.Println(b)  // [72 101 108 108 111 32 87 111 114 108 100]
	
	ss := string(b)              // convert to string
	
	fmt.Println(ss)     // Hello World
}

Type conversion during division

During division, types are converted implicitly. Here are some examples.

package main

import (
	"fmt"
)

func main() {
	a := 6/3        // both are int, a is int
	f := 6.3/3      // float and int, f is float
	
	fmt.Println(a, f)     // 2 2.1
}

Drawbacks of type-casting in Golang

Type conversion is a great way to change the original data type. But unfortunately, in some cases, it loses precision. It should be done sparingly. The needs of type conversion lie in the fact Go doesn’t support implicit type conversions. So in many cases, it should be done separately.