GoLang - Type Casting

2 minute read

In this tutorial, you will learn about Type casting in Go Programming with the help of example.

In Go programming, type casting, also known as type conversion, allows you to convert a value from one data type to another. It enables you to perform operations or assignments involving different types of data. In this tutorial, we will explore type casting in Go programming, along with examples, using simple and easy-to-understand language. So let’s get started and learn about type casting in Go!

Implicit Type Conversion

Go programming provides implicit type conversion for certain cases where the conversion is safe and does not result in a loss of data. For example, converting an integer to a float.

Example:

package main

import "fmt"

func main() {
    var x = 10
    var y = 3.14

    var result = float64(x) + y

    fmt.Println("Result:", result)
}

In the example above, we have an integer variable 'x' and a float variable 'y'. We use type casting to convert 'x' from an integer to a float using 'float64(x)'. The result is stored in the variable 'result', which is then printed.

Explicit Type Conversion

In other cases, when the type conversion is not implicitly done by the compiler, you can perform explicit type casting using the desired type in parentheses before the value you want to convert.

Example:

package main

import "fmt"

func main() {
    var x = 3.14
    var y = int(x)

    fmt.Println("Value of x:", x)
    fmt.Println("Value of y:", y)
}

In this example, we have a float variable 'x'. We explicitly convert 'x' to an integer using 'int(x)', and the 'result' is assigned to the variable 'y'. The values of 'x' and 'y' are then printed.

Type Assertion

Type assertion is used to extract the underlying value of an interface variable and perform operations on it by explicitly specifying its type.

Example:

package main

import "fmt"

func main() {
    var val interface{} = "Hello, World!"

    str, ok := val.(string)
    if ok {
        fmt.Println("String Value:", str)
    } else {
        fmt.Println("Not a string!")
    }
}

In this example, we have an interface variable 'val' that holds the value “Hello, World!”. We use type assertion with 'val.(string)' to extract the underlying string value and assign it to the variable 'str'. We also use the second return value 'ok' to check if the type assertion was successful. If the assertion succeeds, we print the string value; otherwise, we indicate that it is not a string.

Conclusion

Type casting in Go programming allows you to convert values from one data type to another, enabling you to perform operations involving different types. In this tutorial, we explored implicit type conversion, explicit type conversion, and type assertion in Go. By understanding these concepts and utilizing type casting appropriately, you can handle data of various types effectively in your Go programs. So go ahead, practice type casting, and enhance your programming capabilities in Go!

Updated: