How to use the strconv.AppendQuoteRuneToASCII() function in Go

Overview

The strconv package’s AppendQuoteRuneToASCII() function is used to append the single-quoted Go character literal, representing r (as generated by the QuoteRuneToASCII() function) to dst and returns the extended buffer.

Syntax

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

Parameters

  • dst is a byte array to which the single-quoted character is to be appended literal.
  • r is the rune character to be appended.

Return value

The function AppendQuoteRuneToASCII() returns the extended buffer after appending the single-quoted character.

Example

The following code shows how to implement the AppendQuoteRuneToASCII() function in Go.

// Using strconv.AppendQuoteRuneToASCII() Function
package main
import (
"fmt"
"strconv"
)
func main() {
x := []byte("Empty")
fmt.Println("Orignal value before AppendQuoteRuneToASCII()", string(x))
fmt.Println()
// Append a character (rune 108 is the Unicode of l)
x = strconv.AppendQuoteRuneToASCII(x, 108)
fmt.Println("The new value after AppendQuoteRuneToASCII()", string(x))
fmt.Println()
// Append emoji
x = strconv.AppendQuoteRuneToASCII(x, '🥰')
x = strconv.AppendQuoteRuneToASCII(x, '🙉')
fmt.Println("The new Values in the array after AppendQuoteRuneToASCII()", string(x))
fmt.Println()
}

Explanation

  • Line 4: We add the package main.
  • Lines 6–9: We import the necessary packages in the Go project.
  • Lines 11–21: We define the main() function, the variable r of the type rune, assign a value to it, and pass the variable in the AppendQuoteRuneToASCII() function. This returns a single-quoted Go character literal, representing the given rune value. Finally, we display the result.

Note: All the values assigned to variable x are appended to the dst.

Free Resources