Here are the six ways to check if a string integer in Go:
- Using strconv.Atoi()
- Using a Regular Expression
- Using strconv.ParseInt()
- Using unicode.IsDigit()
- Using govalidator library
- Using a loop to check each character
Method 1: Using strconv.Atoi()
The strconv.Atoi() function tries to convert a string to an integer. If the conversion is successful, it returns the integer and nil error. If it fails, it returns a non-nil error.
package main
import (
"fmt"
"strconv"
)
func isNumeric(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
func main() {
fmt.Println(isNumeric("1234"))
fmt.Println(isNumeric("12.34"))
}
Output
true
false
Method 2: Using a Regular Expression
You can use a regular expression to check if a string matches the pattern of an integer.
package main
import (
"fmt"
"regexp"
)
func isNumeric(s string) bool {
re := regexp.MustCompile(`^\d+$`)
return re.MatchString(s)
}
func main() {
fmt.Println(isNumeric("1234"))
fmt.Println(isNumeric("12.34"))
}
Output
true
false
Method 3: Using strconv.ParseInt()
Similarly to strconv.Atoi() function, you can use strconv.ParseInt() to try to parse a string into a 64-bit integer.
package main
import (
"fmt"
"strconv"
)
func isNumeric(s string) bool {
_, err := strconv.ParseInt(s, 10, 64)
return err == nil
}
func main() {
fmt.Println(isNumeric("1234"))
fmt.Println(isNumeric("12.34"))
}
Output
true
false
Method 4: Using unicode.IsDigit() function
The unicode.IsDigit() function is a straightforward way to check if all the characters in a string are numeric digits. The unicode.IsDigit() function checks if a rune (Go’s character type) is a digit.
package main
import (
"fmt"
"unicode"
)
func isNumeric(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
return true
}
func main() {
fmt.Println(isNumeric("1234"))
fmt.Println(isNumeric("12.34"))
fmt.Println(isNumeric("12a34"))
}
Output
true
false
false
Method 5: Using the govalidator library
The govalidator library is a popular choice for string validation and sanitization in Go. It provides a wide array of validation functions, including the ability to check if a string is numeric.
Here’s how you can use govalidator to check if a string is numeric or an integer:
First, you need to install the govalidator package:
go get github.com/asaskevich/govalidator
Then, you can use the IsInt() and IsFloat() functions from the govalidator package to check if a string is an integer or a numeric value, respectively:
package main
import (
"fmt"
"github.com/asaskevich/govalidator"
)
func main() {
fmt.Println(govalidator.IsInt("1234"))
fmt.Println(govalidator.IsInt("12.34"))
fmt.Println(govalidator.IsFloat("12.34"))
fmt.Println(govalidator.IsFloat("abc"))
fmt.Println(govalidator.IsFloat("1234"))
}
Output
Method 6: Using a loop to check each character
You can iterate over each character in the string and check if they are digits.
package main
import (
"fmt"
"unicode"
)
func isNumeric(s string) bool {
for _, r := range s {
if !unicode.IsDigit(r) {
return false
}
}
return true
}
func main() {
fmt.Println(isNumeric("1234"))
fmt.Println(isNumeric("12.34"))
}
Output
true
false
That’s it!
Related posts
Check If String is Whitespace in Go
Check If String is Alphanumeric in Go
Check If the String Starts with Specified Prefix in Go
Check If the String Ends with Specified Suffix in Go

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.