How to Use strconv.AppendQuoteRuneToASCII() Function in Golang

Golang strconv.AppendQuoteRuneToASCII() function is used to append a single-quoted character literal representing the rune, as generated by QuoteRuneToASCII, to dst and returns the extended buffer.

Syntax

func AppendQuoteRuneToASCII(dst []byte, x rune) []byte

Parameters

  1. dst: A byte array (or byte slices) in which we must append the single-quoted character literal.
  2. r: Rune character to be appended.

Return Value

The return type of the AppendQuoteRuneToASCII() function is a []byte, and it returns the extended buffer after appending the single-quoted character.

Example 1

package main

import (
  "fmt"
  "strconv"
)

func main() {
  // A slice of bytes
  s := []byte("ASCII quote: ")

  // A rune
  r := '⌘'

  // Convert and append the rune to the byte slice
  s = strconv.AppendQuoteRuneToASCII(s, r)

  // Convert the byte slice back to a string and print it
  fmt.Println(string(s))
}

Output

ASCII quote: '\u2318'

Example 2

package main

import (
  "fmt"
  "strconv"
)

func main() {
  y := []byte("QuoteRuneToASCII_Test:")
  fmt.Println("Before appending...")
  fmt.Println("y:", string(y))
  fmt.Println("Length(y): ", len(y))

  // Appending characters & various emojis
  y = strconv.AppendQuoteRuneToASCII(y, 98)
  y = strconv.AppendQuoteRuneToASCII(y, '????')
  y = strconv.AppendQuoteRuneToASCII(y, '????')
  y = strconv.AppendQuoteRuneToASCII(y, '????')

  fmt.Println("After appending...")
  fmt.Println("y:", string(y))
  fmt.Println("Length(y): ", len(y))
}

Output

Before appending...
y: QuoteRuneToASCII_Test:
Length(y): 22
After appending...
y: QuoteRuneToASCII_Test:'b''\U0001f34e''\U0001f34c''\U0001f352'
Length(y): 61

That’s it!