strconv.Atoi() appends <nil> to the end of my number – Golang

Issue

Pretty simple question. I’m using strconv.Atoi to convert a string value to an integer, but it returns the number and then a <nil> value to the end for some reason (Like this: 1 <nil>). I don’t see anything online about this.

Here’s the relevant code:

fmt.Println(strconv.Atoi(tokens[index + 2][4:]))

Any help would be greatly appreciated!

Solution

strconv.Atoi returns an integer and an error

package main
import (
  "fmt"
  "strconv"
)

func main() {
  number, err := strconv.Atoi("5")
  fmt.Println(number)
  fmt.Println(err)
}

Prints

5
<nil>

Answered By – Teemu

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.