How to use strconv.QuoteRuneToASCII() Function in Golang?

Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides a QuoteRuneToASCII() function which is used to find a single-quoted Go string literal representing rune and the returned string uses Go escape sequences (\t, \n, \xFF, \u0100) to non-ASCII characters and non-printable characters defined by IsPrint. To access QuoteRuneToASCII() function you need to import strconv Package in your program with the help of import keyword.

Syntax:

func QuoteRuneToASCII(rn rune) string

Parameter: This function takes one parameter of rune type, i.e., rn.

Return value: This function returns a single-quoted Go string literal which represents rune.

Let us discuss this concept with the help of the given examples:

Example 1:

// Golang program to illustrate 
// strconv.QuoteRuneToASCII() Function
package main

import (
    "fmt"
    "strconv"
)

func main() {

    // Finding a single-quoted Go 
    // string literal representing rune
    // Using QuoteRuneToASCII() function
    r := strconv.QuoteRuneToASCII('♥')
    fmt.Println (r)
    
}

Output:

'\u2665'

Example 2:

// Golang program to illustrate 
// strconv.QuoteRuneToASCII() Function
package main
 
import (
    "fmt"
    "strconv"
)
 
func main() {

    // Finding a single-quoted Go 
    // string literal representing rune
    // Using QuoteRuneToASCII() function
    val1 := strconv.QuoteRuneToASCII('♣')
    fmt.Println("Result 1: ", val1)
    fmt.Println("Length 1: ", len(val1))
   
    val2 := strconv.QuoteRuneToASCII('→')
    fmt.Println("Result 2: ", val2)
    fmt.Println("Length 2: ", len(val2))
    
}

Output:

Result 1:  '\u2663'
Length 1:  8
Result 2:  '\u2192'
Length 2:  8

Contact Us