To convert a String to an Integer in Go, you can use the Atoi
function from the strconv
package.
In this example,
strconv
package.str
with a string value of "111"
.Atoi
function to convert it to an integer and store the result in the variable number
.Atoi
function parses the string and returns the equivalent integer value.number
.package main
import (
"fmt"
"strconv"
)
func main() {
str := "111"
number, _ := strconv.Atoi(str)
fmt.Println("Integer value:", number)
}
Integer value: 111
In this example,
strconv
package.str
with a string value of "999"
.Atoi
function to convert it to an integer and store the result in the variable number
.Atoi
function parses the string and returns the equivalent integer value.number
.package main
import (
"fmt"
"strconv"
)
func main() {
str := "999"
number, _ := strconv.Atoi(str)
fmt.Println("Integer value:", number)
}
Integer value: 999
In this tutorial, we learned How to Convert String to Integer in Go language with well detailed examples.