«Convert string to integer type in Go?» — a question that torments beginners
\n\nEveryone who starts writing in Go eventually hits a wall: you need to take a string like "42" and turn it into a proper number 42. It seems simple, right? But this very question on Stack Overflow gathers thousands of views, and underneath it — dozens of comments with typical errors. Why does this happen?
Go doesn't have the familiar int("42") from Python or JavaScript. Instead, it has the powerful but strict strconv package, which forces you to think about what could go wrong. And this is correct: you can't just "pass" a string into int, because the string might be malformed, empty, or contain extra spaces.
In this article, we'll break down converting a string to a number in Go with real examples, walk through the pitfalls that even experienced developers stumble on, and provide working code templates. Let's go!
\n\n1. The Main Tool: The strconv Package
\n\nIn Go's standard library, the strconv package handles string conversion. It has three main functions for converting a string to an integer:
- \n
Atoi(s string) (int, error)— the simplest option, converts a string toint(bit size depends on the platform: 32 or 64 bits). \n ParseInt(s string, base int, bitSize int) (int64, error)— gives full control: number base (10, 16, 2) and bit size (0, 8, 16, 32, 64). \n ParseUint(s string, base int, bitSize int) (uint64, error)— the same, but for unsigned numbers. \n
The most popular mistake is ignoring the returned error. Let's see what this leads to.
\n\nExample 1: Ignoring the Error — Getting a Bug
\n\npackage main\n\nimport (\n "fmt"\n "strconv"\n)\n\nfunc main() {\n s := "123abc"\n num, _ := strconv.Atoi(s) // Error swallowed!\n fmt.Println(num) // Output: 0 — and you won't even know something went wrong\n}\n\n\nGo has no exceptions; an error is a value. If you ignore it (_), you'll get a zero value (0) and spend hours debugging, wondering why your program behaves strangely. Always handle the error!
2. Top 5 Errors When Converting string to int in Golang
\n\nWe analyzed questions on Stack Overflow and identified the most common problems. Here they are:
\n\nError #1: Spaces and Invisible Characters
\n\nThe string " 42 " or "42\\n" — Atoi will return an error. Go does not tolerate extra characters, unlike some languages that automatically trim the string.
s := " 42 "\nnum, err := strconv.Atoi(s)\nif err != nil {\n fmt.Println("Error:", err) // strconv.Atoi: parsing " 42 ": invalid syntax\n}\n\n\nSolution: use strings.TrimSpace() before conversion.
import "strings"\n\ns := " 42 "\ns = strings.TrimSpace(s)\nnum, err := strconv.Atoi(s)\nif err == nil {\n fmt.Println(num) // 42\n}\n\n\nError #2: Empty String
\n\nAn empty string "" is also an error. Many beginners expect an empty string to turn into 0, but that's not the case.
num, err := strconv.Atoi("")\n// err: strconv.Atoi: parsing "": invalid syntax\n\n\nSolution: check the string length or handle the error explicitly, substituting a default value.
\n\nError #3: Overflow
\n\nIf the string contains a number that doesn't fit into the chosen type, Atoi or ParseInt will return an ErrRange error and the maximum/minimum value for that type.
s := "99999999999999999999" // larger than int64\nnum, err := strconv.ParseInt(s, 10, 64)\nif er