How to use the strconv.QuoteRune() function in Golang

Golang strconv.QuoteRune() function

The strconv package includes the QuoteRune() function, which get a single-quoted Go character literal representing the rune, r. For control characters and non-printable characters, the resulting string employs Go escape sequences (\t, \n, \xFF, \u0100) as defined by the IsPrint() function.

Syntax

func QuoteRune(r rune) string

Parameter

  • r: This is the rune value to be returned with a single-quoted Go character literal representing the rune, r.

Return value

The QuoteRune() function return type is a string. It returns a single-quoted Go character literal representing the given rune value.

Code

The following code shows the use of the QuoteRune() function in Golang:

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

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

Code explanation

We declare a variable, str, of the rune type and assign a value to it. Then, we pass the variable to the QuoteRune() function, which returns a single-quoted Go character literal.

Free Resources