Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang
strconv
communitycreator

What is the strconv.QuoteRuneToASCII() 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.

The QuoteRuneToASCII() function

The strconv package in Golang includes the QuoteRuneToASCII() function. This function gets a single-quoted Go character to represent the rune r.

For non-ASCII and non-printable characters, the resulting string employs Go escape sequences (\t, \n, \xFF, and \u0100) as defined by the IsPrint() function.

Syntax

func QuoteRuneToASCII(r rune) string

Parameter

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

Return type

The strconv.QuoteRuneToASCII() function returns a string.

Return value

The QuoteRuneToASCII() function returns a single-quoted Go character literal representing the specific rune value.

Code

The following code shows how to implement the QuoteRuneToASCII() function in Golang.

Note: To use the QuoteRuneToASCII() function, we must first import the strconv Package into your program.

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

Code explanation

  • Line 4: We add the package main.

  • Lines 6 to 9: We import the necessary packages in the Golang project.

  • Lines 11 to 21: We define the main() function, the variable str of the type rune, assign a value to it, and pass the variable in the QuoteRuneToASCII() function. This returns a single-quoted Go character literal representing the given rune value. Finally, we display the result.

RELATED TAGS

golang
strconv
communitycreator

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