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).
func QuoteRuneToGraphic(r rune) string
r: The rune value is to be returned with a single-quoted Go character literal which represents the rune r.A string type is returned.
The QuoteRuneToGraphic() function returns a single-quoted Go character literal which represents the given rune value.
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'))}
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.