This recipe will show you how to convert the strings containing numbers to a numeric type (integer or floating-point value).
Converting strings to numbers
How to do it...
- Open the console and create the folder chapter03/recipe01.
- Navigate to the directory.
- Create the main.go file with the following content:
package main
import (
"fmt"
"strconv"
)
const bin = "00001"
const hex = "2f"
const intString = "12"
const floatString = "12.3"
func main() {
// Decimals
res, err := strconv.Atoi(intString)
if err != nil {
panic(err)
}
fmt.Printf(...