How to use the unicode.IsPrint() function in Golang

Golang unicode.IsPrint() function

The IsPrint() function in the Unicode package is used to determine whether the provided rune r is defined as a printable character in the Go language.

These characters include letters, markings, numerals, punctuation, symbols, and ASCII space characters from the categories L, M, N, P, S, and the ASCII space character.

Note: All the Unicode is divided into various categories.

Syntax

func IsPrint(r rune) bool

Parameter

  • r : The rune type value to be checked whether it is a printable character or not.

Return type

It returns a boolean type value.

Return value

The unicode.IsPrint() function returns true if rune r is a printable character. Otherwise, false.

Note: We must import the Unicode package in the program.

Code

The following code shows how to use the unicode.IsPrint()function:

// Golang program
// Using unicode.IsPrint() Function
package main
import (
"fmt"
"unicode"
)
func main() {
// declare valChar as type rune
var r rune
// declare result as type bool
var result bool
// assigning value
r = 'b'
// checks if the character is printable
// stores the value in variable result
result = unicode.IsPrint(r)
// prints the value
fmt.Printf("\nIs %c a printable character? %t", r, result)
// assigning value
r = '4'
// checks if the character is printable
// stores the value in variable result
result = unicode.IsPrint(r)
// prints the value
fmt.Printf("\nIs %c a printable character? %t",r , result)
// assigning value
r = '?'
// checks if the character is printable
// stores the value in variable result
result = unicode.IsPrint(r)
// prints the value
fmt.Printf("\nIs %c a printable character? %t ",r , result)
// assigning value
r = '~'
// checks if the character is printable
// stores the value in variable result
result = unicode.IsPrint(r)
// prints the value
fmt.Printf("\nIs %c a printable character? %t",r , result)
}

Free Resources