Go: Convert string to int

Use strconv.Atoi to convert a string to an int:

str := "123"
if n, err := strconv.Atoi(str); err == nil {
	fmt.Println(n+1)
} else {
	fmt.Println(str, "is not an integer.")
}
// Output: 124

Comments

Be the first to comment!