To check if a string contains only numeric digits in Go, you can iterate through each character in the string and check if it is a digit using the 'unicode.IsDigit' function.
In this example,
package main
import (
"fmt"
"unicode"
)
func containsOnlyNumericDigits(str string) bool {
for _, char := range str {
if !unicode.IsDigit(char) {
return false
}
}
return true
}
func main() {
str := "12345"
allDigits := containsOnlyNumericDigits(str)
fmt.Println("String contains only numeric digits:", allDigits)
}
String contains only numeric digits: true
In this tutorial, we learned How to Check if String contains only Numeric Digits in Go language with well detailed examples.