This guide explains type conversion in Go, covering integer to float, float to integer, and string conversions using the strconv package.
In this guide, you’ll learn how to perform type conversion (also known as type casting) in Go by converting variables from one data type to another. While converting variables is straightforward, be aware that the actual value may change after conversion due to differences in data representation.
Converting an integer to a floating-point number in Go is simple. You can convert an integer by wrapping it with the desired float type (either float64 or float32).For example, consider the following Go program. It declares an integer variable i, converts it to a float64 stored in variable f, and then prints the result.
Copy
Ask AI
package mainimport "fmt"func main() { var i int = 90 var f float64 = float64(i) fmt.Printf("%.2f\n", f)}
Console output:
Copy
Ask AI
>>> go run main.go90.00
When converting an integer to a float, the numeric value remains the same, but may be represented differently with added precision.
Go also allows you to convert a floating-point number to an integer. This conversion will truncate the decimal portion, losing any fractional precision.Consider this example:
Copy
Ask AI
package mainimport "fmt"func main() { var f float64 = 45.89 var i int = int(f) fmt.Printf("%v\n", i)}
Console output:
Copy
Ask AI
>>> go run main.go45
Here, converting 45.89 to an integer results in 45 because the conversion truncates the fractional part.
Keep in mind that converting a float to an integer results in a loss of precision. Ensure this behavior is acceptable for your application’s use case.
To convert a string to an integer, use the Atoi function, which returns both the converted integer and an error value. When the string represents a valid integer, the error will be nil.For example, consider this conversion of a valid string:
Copy
Ask AI
package mainimport ( "fmt" "strconv")func main() { var s string = "200" i, err := strconv.Atoi(s) fmt.Printf("%v, %T \n", i, i) fmt.Printf("%v, %T", err, err)}
Since “200” is a valid numerical string, the conversion succeeds, and err remains nil.The strconv package comes equipped with helpful functions like Itoa and Atoi for converting between types, with built-in error handling to ensure the conversion is successful.
Now, take a look at an example where the string cannot be converted into an integer:
Copy
Ask AI
package mainimport ( "fmt" "strconv")func main() { var s string = "200abc" i, err := strconv.Atoi(s) fmt.Printf("%v, %T \n", i, i) fmt.Printf("%v, %T", err, err)}
Console output:
Copy
Ask AI
>>> go run main.go0, intstrconv.Atoi: parsing "200abc": invalid syntax, *strconv.NumError
Here, “200abc” is not a valid integer string, so the conversion returns 0 along with an error describing the invalid syntax.
Now that you’ve seen examples of converting between various data types in Go—including integers, floats, and strings—it’s time to put these techniques into practice. Experiment with different data types and conversion methods in your own projects to solidify your understanding of Go’s type conversion mechanisms.Explore more about Go programming on the official Go documentation. Happy coding!