Explain the use of Go's type casting and type conversion for type conversion in Go?
In Go, type casting and type conversion are often used interchangeably, but there is a subtle difference between the two.
Type casting refers to converting a value of one type to another type by explicitly specifying the desired type in the code. For example:
var x float64 = 3.14
var y int = int(x) // cast float64 to int
In this example, we are casting a float64 value to an int value by using the **int()**
function.
Type conversion, on the other hand, refers to converting a value of one type to another type using a conversion function or method. For example:
var s string = "123"
var i, err = strconv.Atoi(s) // convert string to int
In this example, we are converting a string value to an int value by using the **strconv.Atoi()**
function.
In general, type casting is used when the types involved are compatible, and the conversion is a simple matter of changing the type of the value. Type conversion, on the other hand, is used when the types involved are not compatible, and the conversion requires some additional logic or processing to be performed.