xxxxxxxxxx
package main
import (
"fmt"
"strconv"
)
func main() {
// Example string
str := "42"
// Convert string to integer
num, err := strconv.Atoi(str)
if err != nil {
fmt.Println("Error: ", err)
return
}
fmt.Println("Number:", num)
}
xxxxxxxxxx
s := "97"
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
fmt.Printf("%d of type %T", n, n)
}
xxxxxxxxxx
import (
"fmt"
"strconv"
)
func main() {
str := "12345"
num, err := strconv.Atoi(str)
if err == nil {
fmt.Println("String:", str)
fmt.Println("Integer:", num)
} else {
fmt.Println("Error:", err)
}
}
xxxxxxxxxx
package main
import (
"fmt"
"strconv"
)
func main() {
str := "123" // example string
i, err := strconv.Atoi(str)
if err != nil {
fmt.Println("Conversion error:", err)
return
}
fmt.Println(i)
}
xxxxxxxxxx
b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("42", 10, 64)
xxxxxxxxxx
s := "97"
if n, err := strconv.Atoi(s); err == nil {
fmt.Println(n+1)
} else {
fmt.Println(s, "is not an integer.")
}
// Output: 98