How to Check If a String is Integer in Golang [6 Ways]

Here are the six ways to check if a string integer in Go:

  1. Using strconv.Atoi()
  2. Using a Regular Expression
  3. Using strconv.ParseInt()
  4. Using unicode.IsDigit()
  5. Using govalidator library
  6. 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

Using the govalidator library

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