strconv.QuoteRune()
functionThe 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.
func QuoteRune(r rune) string
The QuoteRune()
function return type is a string. It returns a single-quoted Go character literal representing the given rune value.
The following code shows the use of the QuoteRune()
function in Golang:
Note: To use the
QuoteRune()
function, we must first import thestrconv
package into our program.
// Golang program// Using strconv.CanBackquote() Functionpackage mainimport ("fmt""strconv")func main() {// declare a variable of rune typevar str rune// assign valuestr = '☺'// QuoteRune() function returns a single-quoted Go character literal// representing the given rune value.fmt.Println(strconv.QuoteRune(str))// reassign valuestr = '🙉'// QuoteRune() function returns a single-quoted Go character literal// representing the given rune value.fmt.Println(strconv.QuoteRune(str))// reassign valuestr = '♥'// QuoteRune() function returns a single-quoted Go character literal// representing the given rune value.fmt.Println(strconv.QuoteRune(str))}
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.