Type Casting Type Conversion and Type Assertion
Type Casting vs Type Conversion
In computer programming, “type casting” and “type conversion” refer to the process of converting a value from one data type to another.
These terms are often used interchangeably, but they can have slightly different meanings depending on the context.
Type casting refers to the process of explicitly converting a value from one data type to another, typically using a type casting operator or function provided by the programming language.
In most languages, type casting is done at runtime and can involve loss of precision or other undesirable side effects if the value being cast is not compatible with the target data type.
Type conversion, on the other hand, refers to the process of converting a value from one data type to another in a way that is safe and preserves the value of the original data.
Type conversion is often done automatically by the programming language, and does not involve the use of explicit type casting operators or functions.
Here is an example of type casting in Go:
package main
import "fmt"
func main() {
var x int = 10
var y float64 = float64(x) // Type casting int to float64
fmt.Printf("x: %d, y: %f\n", x, y)
var a float64 = 3.14
var b int = int(a) // Type casting float64 to int
fmt.Printf("a: %f, b: %d\n", a, b)
}
In the above example, we have two instances of type casting:
float64(x)is used to cast the value of variablexfrominttofloat64. The result is assigned to variabley. This type casting allows us to convert anintvalue to afloat64value.int(a)is used to cast the value of variableafromfloat64toint. The result is assigned to variableb. This type casting allows us to convert afloat64value to anintvalue.
By performing type casting, we can convert values from one type to another when necessary. However, it’s important to note that type casting may result in loss of precision or truncation of data, depending on the types being converted. Care should be taken to ensure that the casting is done correctly and safely in accordance with the expected behavior.
Overall, type casting and type conversion are important concepts in computer programming that allow you to convert values from one data type to another as needed. Understanding how these concepts work is important for writing correct and efficient code
Type Assertion
Type assertion is used to extract the underlying value of the interface.
i.(T) is the syntax which is used to get the underlying value of interface
func assert( i interface{}){
v , ok := i.(int)
fmt.Println(v.ok)
}
func main(){
var i interface{} = 123
assert(i)
}
Type Casting
Type casting refers to the process of explicitly converting a value from one data type to another
package main
import "fmt
func main() {
x := 10
y := float64(x) // Type cast x to a float64
fmt.Printf("x is of type %T\n", x) // Output: x is of type int
fmt.Printf("y is of type %T\n", y) // Output: y is of type float64
}
Type conversion
Type conversion, on the other hand, refers to the process of converting a value from one data type to another in a way that is safe and preserves the value of the original data.
package main
import "fmt"
func main() {
x := "10"
y, _ := strconv.Atoi(x) // Convert x to an int
fmt.Printf("x is of type %T\n", x) // Output: x is of type string
fmt.Printf("y is of type %T\n", y) // Output: y is of type int
}