Tsallaka zuwa abun ciki
Justice Tech School

Numbers and strconv

strconv converts between strings and numbers. Atoi and ParseFloat return an error as well as a value. Itoa turns an int into text.

Example
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, err := strconv.Atoi("123")
	fmt.Println(n+1, err)
	_, err = strconv.Atoi("abc")
	fmt.Println(err)
	f, _ := strconv.ParseFloat("2.5", 64)
	fmt.Println(f * 2, strconv.Itoa(99)+"!")
}

Result

124 <nil>
strconv.Atoi: parsing "abc": invalid syntax
5 99!

Exercise

Turn text into an int.

n, err := strconv.("12")

Quiz Test

What does Atoi return besides the number?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.