How to Cast in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


if you’re looking to learn how to cast in Golang, you’re in the right place.

Before diving into the code examples, let’s first understand the theory behind casting in Golang.

What is Casting?

Casting in Golang involves converting a value of one type to another type. Golang is a statically typed language, which means that the type of a variable is determined at compile time. Casting is sometimes necessary when working with data of different types, such as when performing arithmetic operations or working with interfaces.

There are two types of casting in Golang: explicit and implicit. Explicit casting involves manually converting a value from one type to another using the cast operator. Implicit casting, also known as type coercion, occurs automatically when Golang converts a value from one type to another to perform an operation.

Here’s how it works:

package main

import "fmt"

func main() {
    var num float64 = 42.5
    fmt.Printf("%v is of type %T\n", num, num)

    var integer int = int(num)
    fmt.Printf("%v is of type %T\n", integer, integer)
}

In the code above, we declare a float64 variable called “num” with the value of 42.5. We then use explicit casting to convert “num” to an integer using the int() function. The resulting integer is stored in a variable called “integer”. We then use the fmt.Printf() function to print the values of both “num” and “integer” along with their types.

Another way to cast in Golang is through type assertion. Type assertion is used when working with interfaces and allows you to extract the underlying value of an interface and cast it to a specific type. Here’s an example:

import "fmt"

func printValue(val interface{}) {
    switch val.(type) {
    case int:
        fmt.Println("The value is an integer:", val.(int))
    case float64:
        fmt.Println("The value is a float:", val.(float64))
    default:
        fmt.Println("Unknown type")
    }
}

func main() {
    printValue(42)
    printValue(3.14)
    printValue("hello")
}

In the code above, we define a function called “printValue” that takes an interface as a parameter. The function uses type assertion to determine the underlying type of the value and prints a message accordingly. We then call the function with different types of values to see the results.

So why would someone want to cast in Golang? One common use case is when working with data of different types, such as when reading data from a file or database. By casting the data to the correct type, you can ensure that it is processed correctly and avoid errors.

In conclusion, casting in Golang is a fundamental operation that you’ll encounter frequently in your Golang programming journey. By using explicit casting or type assertion, you can convert values from one type to another and work with data of different types.

I hope you found this tutorial helpful. If you have any questions or comments, please feel free to leave them below.



Stay up to date on the latest in Coding for AI and Data Science

Intuit Mailchimp