Home »
Golang »
Golang Reference
Golang strconv.Atoi() Function with Examples
Golang | strconv.Atoi() Function: Here, we are going to learn about the Atoi() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 06, 2021
strconv.Atoi()
The Atoi() function is an inbuilt function of the strconv package which is used to convert (interpret) a given string s in the given base (10) and bit size (0) and returns the corresponding integer value. The Atoi() function is equivalent to ParseInt(s, 10, 0), converted to type int.
It accepts one parameter (s string) and returns the corresponding integer value, error (if any).
Syntax
func Atoi(s string) (int, error)
Parameters
- s : A string value that is to be converted into an integer value.
Return Value
The return type of Atoi() function is (int, error), it returns the corresponding integer value, error (if any).
Example 1
// Golang program to demonstrate the
// example of strconv.Atoi() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// Converting string having
// base-10 integer value to integer
fmt.Println(strconv.Atoi("10"))
fmt.Println(strconv.Atoi("-10"))
fmt.Println(strconv.Atoi("56789"))
fmt.Println(strconv.Atoi("-1234"))
// Converting string having
// base-16 integer value to integer
// This conversion will generate an error
fmt.Println(strconv.Atoi("ff"))
fmt.Println(strconv.Atoi("123ab6"))
}
Output:
10 <nil>
-10 <nil>
56789 <nil>
-1234 <nil>
0 strconv.Atoi: parsing "ff": invalid syntax
0 strconv.Atoi: parsing "123ab6": invalid syntax
Example 2
// Golang program to demonstrate the
// example of strconv.Atoi() Function
package main
import (
"fmt"
"strconv"
)
func main() {
var x string
var result int
var err error
x = "108"
result, err = strconv.Atoi(x)
fmt.Println("x:", x)
if err == nil {
fmt.Println("Conversion successful, result:", result)
} else {
fmt.Println("Conversion failed, result:", result)
fmt.Println("Error:", err)
}
x = "-15"
result, err = strconv.Atoi(x)
fmt.Println("x:", x)
if err == nil {
fmt.Println("Conversion successful, result:", result)
} else {
fmt.Println("Conversion failed, result:", result)
fmt.Println("Error:", err)
}
x = "108ffc"
result, err = strconv.Atoi(x)
fmt.Println("x:", x)
if err == nil {
fmt.Println("Conversion successful, result:", result)
} else {
fmt.Println("Conversion failed, result:", result)
fmt.Println("Error:", err)
}
}
Output:
x: 108
Conversion successful, result: 108
x: -15
Conversion successful, result: -15
x: 108ffc
Conversion failed, result: 0
Error: strconv.Atoi: parsing "108ffc": invalid syntax
Example 3:
// Golang program to demonstrate the
// example of strconv.Atoi() Function
package main
import (
"fmt"
"strconv"
)
func main() {
x := "108"
if result, err := strconv.Atoi(x); err == nil {
fmt.Printf("Type(%T), Value(%v)\n", result, result)
}
x = "-12345"
if result, err := strconv.Atoi(x); err == nil {
fmt.Printf("Type(%T), Value(%v)\n", result, result)
}
x = "ff"
if result, err := strconv.Atoi(x); err == nil {
fmt.Printf("Type(%T), Value(%v)\n", result, result)
}
}
Output:
Type(int), Value(108)
Type(int), Value(-12345)
Golang strconv Package »