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 therune 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() Functionpackage mainimport ("fmt""strconv")func main() {// declare a variable of rune typevar str rune// assign valuestr = '☺'// QuoteRuneToGraphic() function returns a single-quoted Go character literal// represents the given rune value.fmt.Println(strconv.QuoteRuneToGraphic(str))// reassign valuestr = '🙉'// QuoteRuneToGraphic() function returns a single-quoted Go character literal// represents the given rune value.fmt.Println(strconv.QuoteRuneToGraphic(str))// reassign valuestr = '♥'// 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 sequencefmt.Println(strconv.QuoteRuneToGraphic('\u000a'))}
Explanation
-
Line 4: We add the
mainpackage. -
Lines 6 to 9: We import necessary packages in Golang project.
-
Line 11 to 21: We define the
main()function, variablestrof rune type and assign a value to it, and pass the variable in theQuoteRuneToGraphic()function which returns a single-quoted Go character literal representing the given rune value. Finally, the result is displayed.