Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Type-Casting in GoLang
2.1.
Types in Go
2.2.
Type-Casting syntax
3.
Implicit Type Conversion in Go
4.
Explicit Type Conversion in Go
4.1.
Example: Go float to int conversion
4.2.
Example: Go int to float conversion
4.3.
Example: Add int and float number using Go type casting
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024

Go Type Casting

Author Sanjana Yadav
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In Golang, typecasting is a method of converting a variable from one data type to another. You can typecast long to int, for example, if you want to save a long value as a simple integer. The cast operator is used to convert values from one type to another.

Type-Casting in GoLang

When we assign the value of one data type to another, we call this type conversion. Implicit Type Conversion is supported by statically typed languages such as C/C++ and Java. However, Golang is unique. It does not support Automatic Type Conversion or Implicit Type Conversion, even if the data types are compatible. The reason for this is that Golang's Strong Type System forbids it. You must convert types explicitly.

Types in Go

In Go, there are numerous data types. Numbers, floating points, booleans, and strings are all examples of data types.

Number: int, int32, int64, uint32, uint64 etc

Floating Points: float32, float64, complex64, complex128

Boolean: bool

String: string

Type-Casting syntax

General type casting has a simple syntax. To convert that value, simply use that other type name as a function.

v:=typeName(otherTypeValue)
e.g. i:= int(28.987) //integer casting
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Implicit Type Conversion in Go

Implicit type conversion, commonly known as coercion, is the compiler's automated type conversion. Coercion is permitted or even required in several languages.

Because of its powerful type system, Golang does not support implicit type conversion.

Explicit Type Conversion in Go

In a given expression, explicit type conversion is a particular programming command that defines what data type a variable (or an intermediate computation result) should be treated as. Casting, for example, will exclude "additional" information (but never adds information to the type being cast).

Example: Go float to int conversion

package main
import "fmt"
func main() {
  var floatValue float32 = 10.65
  // type conversion from float to int
  var intValue int = int(floatValue)
  fmt.Printf("Float Value: %g\n", floatValue)
  fmt.Printf("Integer Value: %d", intValue)
}

Output

Float Value: 10.65
Integer Value: 10

Example: Go int to float conversion

package main
import "fmt"
func main() {
  var intValue int = 10
  // type conversion from int to float
  var floatValue float32 = float32(intValue)
  fmt.Printf("Integer Value: %d\n", intValue)
  fmt.Printf("Float Value: %f", floatValue)
}

Output 

Integer Value: 10
Float Value: 10.000000

Example: Add int and float number using Go type casting

package main
import "fmt"
func main() {
  var num1 int = 90
  var num2 float32 = 8.7
  var sum float32
 
  // addition of different data types
  sum = float32(num1) + num2

  fmt.Printf("Sum is %g",sum)
}

Output

Sum is 98.7

FAQs

  1. How do you convert types of Go?
    The strconv.Itoa method from the strconv package in the Go standard library can be used to convert numbers to strings. If you enter a number or a variable into the method's parenthesis, the numeric value is converted to a string value.
     
  2. What is type assertion in Golang?
    In Golang, type assertions provide access to the actual type of a variable in an interface. If the data type is already present in the interface, it will obtain the interface's real data type value. A type assertion pulls a value of the provided explicit type from an interface value.
     
  3. How do I change variable type in Golang?
    Because Go is a statically typed language, the types of variables must be known at build time, and they cannot be changed at runtime.

Key Takeaways

In this article we have extensively discussed typecasting and its implementation in GoLang. We saw how typecasting is done in Golang with the help of examples.

We hope that this blog has helped you enhance your knowledge regarding typecasting and if you would like to learn more, check out our articles on Typecasting in C.

Don’t stop here, explore Golang with Golang Archives from coding ninjas. Do upvote our blog to help other ninjas grow. Happy Coding!”

Previous article
Go Comments
Next article
Go Decision Making
Live masterclass