Golang strconv.QuoteRuneToASCII
last modified April 20, 2025
This tutorial explains how to use the strconv.QuoteRuneToASCII
function in Go.
We'll cover rune-to-string conversion basics with practical examples.
The strconv.QuoteRuneToASCII function converts a rune to an ASCII-quoted string. It's useful for safely representing runes in ASCII-only contexts.
The function returns a single-quoted string literal representing the rune. Non-ASCII and special characters are escaped using Go escape sequences.
Basic QuoteRuneToASCII Example
The simplest use of strconv.QuoteRuneToASCII
converts a rune to a quoted string.
Here we demonstrate basic ASCII and non-ASCII rune conversion.
package main import ( "fmt" "strconv" ) func main() { r := 'A' quoted := strconv.QuoteRuneToASCII(r) fmt.Printf("Rune '%c' quoted: %s\n", r, quoted) r2 := 'δΈ' quoted2 := strconv.QuoteRuneToASCII(r2) fmt.Printf("Rune '%c' quoted: %s\n", r2, quoted2) }
We convert both ASCII and non-ASCII runes to quoted strings. The non-ASCII rune is escaped using Unicode escape sequences. The output shows the quoted forms.
Handling Special Characters
strconv.QuoteRuneToASCII
properly escapes special characters.
This example shows how control characters are handled.
package main import ( "fmt" "strconv" ) func main() { specialChars := []rune{'\n', '\t', '\'', '\\', '\x00'} for _, r := range specialChars { quoted := strconv.QuoteRuneToASCII(r) fmt.Printf("Rune %U quoted: %s\n", r, quoted) } }
We test various special characters including newline, tab, and null. Each is properly escaped in the output. The function ensures safe representation.
Comparing with QuoteRune
This example compares QuoteRuneToASCII
with QuoteRune
.
The difference is in how non-ASCII characters are handled.
package main import ( "fmt" "strconv" ) func main() { r := 'θͺ' quotedASCII := strconv.QuoteRuneToASCII(r) quotedRegular := strconv.QuoteRune(r) fmt.Println("QuoteRuneToASCII:", quotedASCII) fmt.Println("QuoteRune:", quotedRegular) }
QuoteRuneToASCII
escapes non-ASCII characters while QuoteRune
keeps them as-is. The output shows the difference in representation.
Working with Unicode Values
This example demonstrates how Unicode values outside the Basic Multilingual Plane
are handled by QuoteRuneToASCII
.
package main import ( "fmt" "strconv" ) func main() { highUnicode := []rune{ '\U0001F600', // π '\U0001F680', // π '\U0001F4A9', // π© } for _, r := range highUnicode { quoted := strconv.QuoteRuneToASCII(r) fmt.Printf("Rune %U quoted: %s\n", r, quoted) } }
Emoji and other high Unicode characters are properly escaped. The function
represents them using \U
followed by 8 hex digits.
Generating JSON-Compatible Strings
QuoteRuneToASCII
can help generate JSON-compatible string representations.
This example shows its use in JSON encoding contexts.
package main import ( "fmt" "strconv" ) func main() { runes := []rune{'A', '"', '\\', '\n', 'δΈ'} fmt.Print("[") for i, r := range runes { if i > 0 { fmt.Print(", ") } fmt.Print(strconv.QuoteRuneToASCII(r)) } fmt.Println("]") }
We create a JSON-like array of quoted runes. Special characters are properly escaped, making the output valid JSON. This demonstrates practical usage.
Performance Considerations
For performance-critical code, understanding the overhead of rune quoting is
important. This example benchmarks QuoteRuneToASCII
.
package main import ( "fmt" "strconv" "time" ) func main() { const iterations = 1000000 testRunes := []rune{'A', 'δΈ', '\n', '\U0001F600'} start := time.Now() for i := 0; i < iterations; i++ { for _, r := range testRunes { strconv.QuoteRuneToASCII(r) } } fmt.Println("QuoteRuneToASCII duration:", time.Since(start)) }
The benchmark shows the performance of quoting different rune types. ASCII runes are faster to quote than non-ASCII ones. Consider caching results if needed.
Practical Example: Rune Escaper
This practical example demonstrates using QuoteRuneToASCII
to create
a rune escaper function for safe output.
package main import ( "fmt" "strconv" ) func escapeRunes(input string) string { var result string for _, r := range input { result += strconv.QuoteRuneToASCII(r) } return result } func main() { testString := "Hello\nδΈηπ" fmt.Println("Original:", testString) fmt.Println("Escaped:", escapeRunes(testString)) }
We create a function that escapes all runes in a string. The output shows how each character is safely represented. This is useful for debugging or logging.
Source
Go strconv package documentation
This tutorial covered the strconv.QuoteRuneToASCII
function in Go
with practical examples of rune-to-string conversion in various scenarios.
Author
List all Go tutorials.