Type Conversion in Go:
Type conversion in Go refers to the process of converting a value from one data type to another. This is necessary when performing operations between different types or when assigning values to variables of a different type. Go requires explicit type conversion to ensure type safety.
Example 1: Basic Type Conversion
package main
import "fmt"
func main() {
var num1 int = 42
var num2 float64 = float64(num1) // Convert int to float64
fmt.Println("Original Integer:", num1)
fmt.Println("Converted to Float64:", num2)
}
In this example, an integer (num1
) is converted to a float64 (num2
) to perform a division operation, ensuring that the result is of the correct type.
Example 2: Type Conversion with strconv
package main
import (
"fmt"
"strconv"
)
func main() {
str := "42"
num, err := strconv.Atoi(str) // Convert string to int
if err == nil {
fmt.Println("Converted String to Integer:", num)
} else {
fmt.Println("Conversion Error:", err)
}
}
Here, the strconv.Atoi
function is used to convert a string (str
) to an integer (num
). Error handling is important because the conversion may fail if the string is not a valid integer.
Example 3: Type Conversion with Custom Types
package main
import "fmt"
type Celsius float64
type Fahrenheit float64
func cToF(c Celsius) Fahrenheit {
return Fahrenheit(c*9/5 + 32)
}
func main() {
var c Celsius = 100
f := Fahrenheit(c) // Custom type conversion
fmt.Printf("%v Celsius is equal to %v Fahrenheit\n", c, f)
}
In this example, custom types (Celsius
and Fahrenheit
) are defined, and a function (cToF
) is created to convert Celsius to Fahrenheit. The conversion is then used in the main
function.
Type Inference in Go:
Type inference in Go is the ability of the compiler to deduce the type of a variable based on its value. While Go is a statically typed language, type inference reduces the need for explicit type declarations, making the code more concise.
Example 1: Variable Declaration with Type Inference
package main
import "fmt"
func main() {
// Type inference in variable declaration
message := "Hello, Go!"
fmt.Println(message)
}
Here, the type of the variable message
is inferred based on the assigned value ("Hello, Go!"). The compiler automatically assigns the type string
to the variable.
Example 2: Type Inference in Function Return Types
package main
import "fmt"
// Type inference in function return type
func add(a, b int) int {
return a + b
}
func main() {
result := add(3, 5)
fmt.Println("Sum:", result)
}
The function add
does not explicitly declare the return type; however, the compiler infers it based on the type of the expression a + b
, which is int
.
Example 3: Type Inference in Slices
package main
import "fmt"
func main() {
// Type inference in slices
numbers := []int{1, 2, 3, 4, 5}
fmt.Println("Numbers:", numbers)
}
In this example, the type of the numbers
variable is inferred based on the provided values. The compiler recognizes that it is a slice of integers.
Type conversion and type inference are essential concepts in Go, contributing to the language's safety, expressiveness, and readability. Type conversion ensures correct operations between different types, while type inference reduces verbosity by allowing the compiler to deduce types in certain contexts. These features collectively enhance the developer experience in Go programming.