AppDividend
Latest Code Tutorials

Golang Cast: How to Do TypeCasting in Golang

Type casting happens when we assign a value of one data type to another data type. Statically typed languages like C++ and Java provide the support for Implicit Type Conversion. Still, the Go language is different, as it doesn’t support an Automatic Type Conversion or Implicit Type Conversion even if the data types are compatible.

Golang cast

Typecasting in Golang is a way to convert the variable from one data type to another data type. For instance, if you want to save the long value into a simple integer, you can typecast long to int. You can convert the values from one type to another using the cast operator.

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

Types of Golang Typecasting

  1. Implicit Type Conversion
  2. Explicit Type Conversion

Golang Implicit Type Casting

Implicit type conversion, also known as coercion in Golang 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.

Golang 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 adds information to the type being cast).

See the following code snippets of explicit type casting in Go.

var badboys int = 1921

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

var badboys3 int64 = int64(badboys)

var badboys4 uint = uint(badboys)

If you want to convert a value from one data type to another, you must follow the following syntax.

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.

See the following complete code example.

// hello.go

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

go run hello.go
Multiplication = 399.000000

From the output, you can see that.

Golang has a strong 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.

See the following code.

// hello.go

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

go run hello.go
# command-line-arguments
./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.

That’s it for Golang casting.

Related posts

How to Convert Golang String to Byte Array

How to Convert JSON to Map in Golang

 How To Convert String To Rune in Go

Leave A Reply

Your email address will not be published.

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