Contents
Roadmap info from roadmap website
Type Casting
Go doesnβt support automatic type conversion, but it allows type casting, which is the process of explicitly changing the variable type.
-
Basic Type Conversion: Convert between basic types like
int
andfloat64
using explicit type conversion. Example:var floatVal float64 = float64(intVal)
-
String Conversion: Use functions like
strconv.Atoi
andstrconv.Itoa
for converting between strings and integers. -
Type Assertion: Retrieve underlying types from interfaces using type assertions. Example: `strVal, ok := i.(string)
-
Custom Type Conversion: Define custom types and methods to handle conversions within your application.
package main
import (
"fmt"
)
type Celsius float64
type Fahrenheit float64
func (c Celsius) ToFahrenheit() Fahrenheit {
return Fahrenheit(c*9/5 + 32)
}
func main() {
tempC := Celsius(25)
tempF := tempC.ToFahrenheit()
fmt.Printf("%.2fΒ°C is %.2fΒ°F\n", tempC, tempF)
}