GO Program to check the character is a digit

For a better understanding of this example, we always recommend you to learn the basic topics of Golang programming listed below:

How to check the character is a digit or not

If you want to know if a character is a Unicode letter or digit, then use the Character. isLetter and Character. isDigit methods.

func IsDigit(r rune) bool 

IsDigit reports whether the given rune is a decimal digit

 

How to check the character is a digit in Go Program

Here we are showing how to check the character is a digit in the Go language. Here variable chr is used to hold the read character. For reading the character create the reader using NewReader. Check the character read is a digit or not by using unicode.IsDigit(chr). Finally print the results. Given below are the steps which are used in the Go program. 

ALGORITHM

STEP 1: Import the package fmt, bufio, os, unicode

STEP 2: Start function main()

STEP 3: Create a reader using NewReader as reader := bufio.NewReader(os.Stdin)

STEP 4: Read the character into variable chr as chr, _, _ := reader.ReadRune()

STEP 5: Check the character is an alphabet or not by using unicode.IsDigit(chr)

STEP 6: Use if statement for printing proper message

GO Source Code

                                          package main

import (
    "bufio"
    "fmt"
    "os"
    "unicode"
)

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter any character to be checked = ")
    chr, _, _ := reader.ReadRune()

    if unicode.IsDigit(chr) {
        fmt.Printf("%c is a Digit\n", chr)
    } else {
        fmt.Printf("%c is not a Digit\n", chr)
    }
}
                                      

OUTPUT

Enter any character to be checked = 6
6 is a Digit

Enter any character to be checked = c
c is not a Digit