Prerequisite: Golang Data Types
Type conversion happens when we assign the value of one data type to another. Statically typed languages like C/C++, Java, provide the support for Implict Type Conversion but Golang is different, as it doesn’t support the Automatic Type Conversion or Implicit Type Conversion even if the data types are compatible. The reason for this is the Strong Type System of the Golang which doesn’t allow to do this. For type conversion, you must perform explicit conversion.
As per Golang Specification, there is no typecasting word or terminology in Golang. If you will try to search Type Casting in Golang Specifications or Documentation, you will find nothing like this. There is only Type Conversion. In Other programming languages, typecasting is also termed as the type conversion.
What is the need for Type Conversion?
Well, if you need to take advantage of certain characteristics of data type hierarchies, then we have to change entities from one data type into another. The general syntax for converting a value val to a type T is T(val).
var geek1 int = 845 // explicit type conversion var geek2 float64 = float64(geek1) var geek3 int64 = int64(geek1) var geek4 uint = uint(geek1)
Average = 44.526318
Note: As Golang has a strong type system, it doesn’t allow to mix(like addition, subtraction, multiplication, division, etc.) the numeric types in the expressions and also you are not allowed to perform an assignment between the two mixed types.
var geek1 int64 = 875 // it will give compile time error as we // are performing an assignment between // mixed types i.e. int64 as int type var geek2 int = geek1 var geek3 int = 100 // it gives compile time error // as this is invalid operation // because types are mix i.e. // int64 and int var addition = geek1 + geek3
- How to add a method to struct type in Golang?
- Converting a string variable into Boolean, Integer or Float type in Golang
- Searching an element of float64 type in Golang slice
- Golang | Searching an element of int type in slice of ints
- Searching an element of string type in Golang slice
- Type Switches in GoLang
- How to Get Float64 Type Random Number in Golang?
- How to Get Float32 Type Random Number in Golang?
- How to Get Int Type Random Number in Golang?
- How to Get Int31 Type Random Number in Golang?
- How to Get Int31n Type Random Number in Golang?
- How to Get Uint32 Type Random Number in Golang?
- How to Get Uint64 Type Random Number in Golang?
- How to Get Intn Type Random Number in Golang?
- How to Get Int63n Type Random Number in Golang?
- How to Convert string to integer type in Golang?
- Different Ways to Find the Type of an Object in Golang
- How to find the type of Struct in Golang?
- reflect.Type() Function in Golang with Examples
- How to convert Int data type to Float in Golang?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.