strconv.Atoi, strconv.Itoa
func Atoi(s string) (int, error)
func Itoa(i int) stringParse a base-10 integer into an int, or format an int as a base-10 string.
| Parameter | Description |
|---|---|
s | A decimal integer, optionally with a leading sign. Leading zeros are allowed; a trailing unit such as px is not. |
i | The integer to format. Itoa does not return an error. |
Returns: Atoi returns the parsed int and a nil error on success. Itoa returns the decimal string.
Errors
*strconv.NumError— On failure, Atoi returns 0 and a *NumError. NumError.Err is strconv.ErrSyntax when the text is not a decimal integer, or strconv.ErrRange when the value does not fit in an int.
- Atoi("80px") is a syntax error, not a successful parse of 80. There is no equivalent of parseInt stopping at the first non-digit.
package main
import (
"fmt"
"strconv"
)
func main() {
n, err := strconv.Atoi("80")
fmt.Println(n, err)
_, err = strconv.Atoi("80px")
fmt.Println(err)
fmt.Println(strconv.Itoa(-80))
}Expected output
80 <nil>
strconv.Atoi: parsing "80px": invalid syntax
-80