How to Use strconv.IsPrint() Function in Golang

Golang strconv.IsPrint() function is “used to check whether a given rune is defined as printable.” 

The print characters include the following:

  1. letters
  2. numerals
  3. punctuation
  4. symbols
  5. ASCII space

Syntax

func IsPrint(r rune) bool

Parameters

r: This is the rune value to be checked.

Return value

The strconv.IsPrint() function returns a boolean true if the given rune is defined as a printable. Otherwise, false.

Example 1: How does IsPrint() function work?

package main

import (
  "fmt"
  "strconv"
)

func main() {
  fmt.Println(strconv.IsPrint('x'))
  fmt.Println(strconv.IsPrint('?'))
}

Output

true
true

Example 2: How to Use strconv.IsPrint() function

package main

import (
  "fmt"
  "strconv"
)

func main() {
  data := 'k'
  res1 := strconv.IsPrint(data)
  fmt.Printf("Result 1: %v", res1)

  val := '\003'
  res3 := strconv.IsPrint(val)
  fmt.Printf("\nResult 3: %v", res3)
}

Output

Result 1: true
Result 3: false

That’s it.

Leave a Comment