Type casting, also known as type conversion, is the process of converting a value of one type to another compatible type in Go. It allows you to change the interpretation or representation of a value, enabling you to perform operations or assignments that require a specific type.
In Go, type casting is done explicitly using syntax like Type(expression)
, where Type
represents the target type to which you want to convert the expression. However, it’s important to note that not all types can be converted to each other, and conversions should be done only between compatible types.
There are two main types of type casting in Go:
Conversion between numeric types:
- Converting a value of one numeric type to another compatible numeric type, such as converting an
int
to afloat64
or vice versa. - Example:
float64(42)
orint(3.14)
Conversion between non-numeric types:
- Converting values between non-numeric types, such as converting a
string
to anint
or anint
to astring
. - Example:
strconv.Atoi("123")
orstrconv.Itoa(456)
It’s important to note that type casting doesn’t change the underlying value; it only changes how the value is interpreted. In some cases, type casting may result in a loss of precision or truncation of data, especially when converting between numeric types.
It’s recommended to handle type casting carefully, check for potential errors, and be aware of the compatibility rules and limitations when performing type conversions in Go.
Type Casting or Type Conversion Examples.
Certainly! Here’s a comprehensive list of type casting or type conversion examples in Go, covering various types:
- Numeric Type Conversions:
package main
import "fmt"
func main() {
var numInt int = 42
var numFloat float64 = float64(numInt) // int to float64
fmt.Println(numFloat)
var numFloat32 float32 = 3.14
var numInt32 int32 = int32(numFloat32) // float32 to int32
fmt.Println(numInt32)
}
- String Conversions:
package main
import (
"fmt"
"strconv"
)
func main() {
var numInt int = 42
var numStr string = strconv.Itoa(numInt) // int to string
fmt.Println(numStr)
var numStr2 string = "123"
var numInt2, _ = strconv.Atoi(numStr2) // string to int
fmt.Println(numInt2)
}
- Rune and Byte Conversions:
package main
import "fmt"
func main() {
var runeValue rune = 'A'
var intValue int = int(runeValue) // rune to int
fmt.Println(intValue)
var byteValue byte = 65
var intValue2 int = int(byteValue) // byte to int
fmt.Println(intValue2)
}
- Pointer Type Conversions:
package main
import (
"fmt"
"unsafe"
)
func main() {
var numInt int = 42
var ptrUnsafePointer unsafe.Pointer = unsafe.Pointer(&numInt) // *int to unsafe.Pointer
fmt.Println(ptrUnsafePointer)
var ptrInt *int = (*int)(ptrUnsafePointer) // unsafe.Pointer to *int
fmt.Println(ptrInt)
}
These examples showcase different type conversions in Go, including numeric types, string conversions, rune and byte conversions, and pointer type conversions. Please note that some conversions may require additional error handling or consideration for precision and data loss.