Golang strconv.QuoteRuneToASCII() Function with Examples

Golang | strconv.QuoteRuneToASCII() Function: Here, we are going to learn about the QuoteRuneToASCII() function of the strconv package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 10, 2021

strconv.QuoteRuneToASCII()

The QuoteRuneToASCII() function is an inbuilt function of the strconv package which is used to get a single-quoted Go character literal representing the rune r. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for non-ASCII characters and non-printable characters as defined by IsPrint() function.

It accepts a parameter (r) and returns a single-quoted Go character literal representing the rune r.

Syntax:

func QuoteRuneToASCII(r rune) string

Parameter(s):

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

Return Value:

The return type of the QuoteRuneToASCII() function is a string, it returns a single-quoted Go character literal representing the given rune value.

Example 1:

// Golang program to demonstrate the
// example of strconv.QuoteRuneToASCII() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(strconv.QuoteRuneToASCII('☺'))
	fmt.Println(strconv.QuoteRuneToASCII('🙈'))
	fmt.Println(strconv.QuoteRuneToASCII('🙉'))
	fmt.Println(strconv.QuoteRuneToASCII('🙊'))
}

Output:

'\u263a'
'\U0001f648'
'\U0001f649'
'\U0001f64a'

Example 2:

// Golang program to demonstrate the
// example of strconv.QuoteRuneToASCII() Function

package main

import (
	"fmt"
	"strconv"
)

func main() {
	var x rune
	var result string

	x = '☺'
	result = strconv.QuoteRuneToASCII(x)
	fmt.Printf("%T, %c\n", x, x)
	fmt.Printf("%T, %v\n", result, result)

	x = '♥'
	result = strconv.QuoteRuneToASCII(x)
	fmt.Printf("%T, %c\n", x, x)
	fmt.Printf("%T, %v\n", result, result)
}

Output:

int32, ☺
string, '\u263a'
int32, ♥
string, '\u2665'

Golang strconv Package »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.