Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
strconv
communitycreator
function

What is strconv.QuoteRuneToGraphic() Function in Golang

Maria Elijah

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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'))
}
Using strconv.QuoteRuneToGraphic() function

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.

RELATED TAGS

golang
strconv
communitycreator
function

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring