How to Use strconv Package in Go: Convert Strings and Numbers Easily
The
strconv package in Go provides functions to convert strings to basic data types like integers and floats, and to convert these types back to strings. Use functions like strconv.Atoi to convert a string to an integer, and strconv.Itoa to convert an integer to a string.Syntax
The strconv package offers functions for conversions between strings and basic types.
strconv.Atoi(string) (int, error): Converts a string to an integer.strconv.Itoa(int) string: Converts an integer to a string.strconv.ParseBool(string) (bool, error): Converts a string to a boolean.strconv.FormatBool(bool) string: Converts a boolean to a string.strconv.ParseFloat(string, bitSize int) (float64, error): Converts a string to a float64 or float32.strconv.FormatFloat(float64, fmt byte, prec int, bitSize int) string: Converts a float to a string.
go
package main import ( "fmt" "strconv" ) func main() { // Convert string to int i, err := strconv.Atoi("123") if err != nil { fmt.Println("Error:", err) } fmt.Println("Integer:", i) // Convert int to string s := strconv.Itoa(456) fmt.Println("String:", s) }
Output
Integer: 123
String: 456
Example
This example shows how to convert strings to integers and booleans, and back to strings using strconv. It also handles errors when conversion fails.
go
package main import ( "fmt" "strconv" ) func main() { // String to int numStr := "42" num, err := strconv.Atoi(numStr) if err != nil { fmt.Println("Conversion error:", err) } else { fmt.Println("Converted integer:", num) } // Int to string str := strconv.Itoa(100) fmt.Println("Converted string:", str) // String to bool boolStr := "true" b, err := strconv.ParseBool(boolStr) if err != nil { fmt.Println("Bool conversion error:", err) } else { fmt.Println("Converted bool:", b) } // Bool to string boolToStr := strconv.FormatBool(false) fmt.Println("Bool to string:", boolToStr) }
Output
Converted integer: 42
Converted string: 100
Converted bool: true
Bool to string: false
Common Pitfalls
Common mistakes when using strconv include:
- Ignoring errors returned by conversion functions, which can cause unexpected behavior.
- Using
strconv.Atoion strings that are not valid integers, leading to errors. - Confusing
strconv.Itoa(int to string) withstrconv.Atoi(string to int). - Not specifying the correct bit size when parsing floats with
strconv.ParseFloat.
go
package main import ( "fmt" "strconv" ) func main() { // Wrong: ignoring error num, _ := strconv.Atoi("abc") // "abc" is not a number fmt.Println("Number:", num) // Prints 0 silently // Right: check error num2, err := strconv.Atoi("abc") if err != nil { fmt.Println("Error detected:", err) } else { fmt.Println("Number:", num2) } }
Output
Number: 0
Error detected: strconv.Atoi: parsing "abc": invalid syntax
Quick Reference
| Function | Description | Example |
|---|---|---|
| strconv.Atoi(string) | Convert string to int | i, err := strconv.Atoi("123") |
| strconv.Itoa(int) | Convert int to string | s := strconv.Itoa(123) |
| strconv.ParseBool(string) | Convert string to bool | b, err := strconv.ParseBool("true") |
| strconv.FormatBool(bool) | Convert bool to string | s := strconv.FormatBool(true) |
| strconv.ParseFloat(string, bitSize int) | Convert string to float64 or float32 | f, err := strconv.ParseFloat("3.14", 64) |
| strconv.FormatFloat(float64, fmt byte, prec int, bitSize int) | Convert float to string | s := strconv.FormatFloat(3.14, 'f', 2, 64) |
Key Takeaways
Always check errors returned by strconv conversion functions to avoid silent failures.
Use strconv.Atoi and strconv.Itoa for converting between strings and integers.
Use strconv.ParseBool and strconv.FormatBool for boolean conversions.
For floats, use strconv.ParseFloat and strconv.FormatFloat with correct bit sizes.
The strconv package is essential for safe and easy string and number conversions in Go.