What is the strconv.IsPrint() function in Golang?

Golang strconv.IsPrint() function

The IsPrint() function of the strconv package is used to check whether a given rune is defined as printable by Golang.

The print characters include the following:

  • letters
  • numerals
  • punctuation
  • symbols
  • ASCII space

To use the IsPrint() function, we use the import keyword to import the strconv package into our program.

Syntax


func IsPrint(r rune) bool

Parameter

  • r: This is the rune value to be checked.

Return value

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

Code

The following code shows how to use the strconv.IsPrint() in Golang.

package main
import (
"fmt" // format package
"strconv"
)
func main() {
fmt.Println(strconv.IsPrint('x'))
fmt.Println(strconv.IsPrint('?'))
fmt.Println(strconv.IsPrint('-'))
fmt.Println(strconv.IsPrint('\t'))
fmt.Println(strconv.IsPrint('6'))
fmt.Println(strconv.IsPrint('~'))
fmt.Println(strconv.IsPrint('\007'))
}

Explanation

  • Line 1: We add the package main.

  • Lines 3-6: We import the necessary packages in our Golang project.

  • Lines 8-16: We define the main() function and check whether the rune is printable using IsPrint() by passing different symbols as parameters.

Free Resources