Number Parsing
// Importing necessary packages.
import (
"fmt"
"strconv"
)
// The main function, where the execution of the program begins.
func main() {
// Parsing a floating-point number from a string.
f, _ := strconv.ParseFloat("1.234", 64)
fmt.Println(f)
// Parsing an integer from a string.
i, _ := strconv.ParseInt("123", 0, 64)
fmt.Println(i)
// Parsing a hexadecimal integer from a string.
d, _ := strconv.ParseInt("0x1c8", 0, 64)
fmt.Println(d)
// Parsing an unsigned integer from a string.
u, _ := strconv.ParseUint("789", 0, 64)
fmt.Println(u)
// Parsing an integer from a string using Atoi.
k, _ := strconv.Atoi("135")
fmt.Println(k)
// Attempting to parse an invalid string to an integer (results in an error).
_, e := strconv.Atoi("wat")
fmt.Println(e)
}Output
Last updated