What is strconv.QuoteRuneToGraphic() Function in Golang

Overview

The strconv package includes the QuoteRuneToGraphic() function used to get a single-quoted Go character literal representing the rune r. The resulting string will utilize a Go escape sequence if the rune is not a Unicode graphic character, as described by the IsGraphic() function (\t, \n, \xFF, \u0100).

Syntax

func QuoteRuneToGraphic(r rune) string

Parameter

  • r: The rune value is to be returned with a single-quoted Go character literal which represents the rune r.

Return type

A string type is returned.

Return value

The QuoteRuneToGraphic() function returns a single-quoted Go character literal which represents the given rune value.

Code

The following code shows how to implement the QuoteRuneToGraphic() function in Golang:

Note: To use the QuoteRuneToGraphic() function, we must first import the strconv Package into our program.

// Golang program
// Using strconv.QuoteRuneToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
// declare a variable of rune type
var str rune
// assign value
str = '☺'
// QuoteRuneToGraphic() function returns a single-quoted Go character literal
// represents the given rune value.
fmt.Println(strconv.QuoteRuneToGraphic(str))
// reassign value
str = '🙉'
// QuoteRuneToGraphic() function returns a single-quoted Go character literal
// represents the given rune value.
fmt.Println(strconv.QuoteRuneToGraphic(str))
// reassign value
str = '♥'
// QuoteRuneToGraphic() function returns a single-quoted Go character literal
// represents the given rune value.
fmt.Println(strconv.QuoteRuneToGraphic(str))
fmt.Println("************")
// displays the Go excape sequence
fmt.Println(strconv.QuoteRuneToGraphic('\u000a'))
}

Explanation

  • Line 4: We add the main package.

  • Lines 6 to 9: We import necessary packages in Golang project.

  • Line 11 to 21: We define the main() function, variable str of rune type and assign a value to it, and pass the variable in the QuoteRuneToGraphic() function which returns a single-quoted Go character literal representing the given rune value. Finally, the result is displayed.

Free Resources