TypeCasting in Golang

Typecasting in Go refers to the process of converting a value of one type to another type. This conversion can be done automatically or explicitly by the programmer.

For example, you can typecast long to int to save the long value into a simple integer. Using the cast operator, you can convert the values from one type to another.

In short, if you want to take advantage of specific characteristics of data type hierarchies, you have to change the entities from one data type into another.

There are two types of conversion.

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Implicit Type Casting

Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler.

Golang does not support implicit type conversion because of its robust type system.

Some languages allow or even require compilers to provide coercion.

Explicit Type Casting

Explicit type conversion is a special programming instruction that specifies what data type to treat a variable (or an intermediate calculation result) in a given expression.

For example, casting will ignore “extra” information (but never add information to the type being cast).

var badboys int = 1921

// explicit type conversion
var badboys2 float64 = float64(badboys)

var badboys3 int64 = int64(badboys)

var badboys4 uint = uint(badboys)

Syntax of Type Conversions

You must follow the following syntax to convert a value from one data type to another.

newDataTypeVariable = T(oldDataTypeVariable)

Where T is the new data type.

Some numeric conversions.

Unlike in C, in Go, assignment between items of different types requires an explicit conversion.

Try removing the float64 or uint conversions in the example and see what happens.

Example 1

package main

import "fmt"

func main() {
  // taking the required
  // data into variables
  var x int = 19
  var y int = 21
  var mul float32

  // explicit type conversion
  mul = float32(x) * float32(y)

  // Displaying the result
  fmt.Printf("Multiplication = %f\n", mul)
}

Output

Multiplication = 399.000000

From the output, you can see that.

Go has a robust type system; that is why it doesn’t allow you to mix the numeric types in the expressions, and also, you are not allowed to perform the assignment between the two mixed types.

Example 2

package main

import "fmt"

func main() {

  // taking the required
  // data into variables
  var x int = 19
  var y float32 = 21
  var mul float32

  mul = x * y

  // Displaying the result
  fmt.Printf("Multiplication = %f\n", mul)
}

Output

./hello.go:14:10: invalid operation: x * y (mismatched types int and float32)

It gives a compile-time error because this is an invalid operation. After all, types are a mix.

Type assertion

Go also supports type assertion, a mechanism for extracting the concrete type from an interface value.

package main

import "fmt"

type Person struct {
  FirstName string `json:"firstName"`
  LastName string `json:"lastName"`
}

func main() {
  var val interface{} = "hello"
  s := val.(string)
  fmt.Println(s)
}

Output

hello

That’s it.

1 thought on “TypeCasting in Golang”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.