What is the strconv.AppendQuoteRuneToGraphic() function in Go?

Overview

The AppendQuoteRuneToGraphic() function of the strconv package is used to append the single-quoted Go character literal that represents r (as generated by the QuoteRuneToGraphic() function) to dst and then return the extended buffer.

Note: The parameter dst is of type []byte and r is of the type rune.

Syntax

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

Parameter values

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

Return value

The AppendQuoteRuneToGraphic() function returns the extended buffer after appending the single-quoted character to dst.

Example

The following code shows us how to implement the AppendQuoteRuneToGraphic() function in Golang.

// Using strconv.AppendQuoteRuneToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
x := []byte("QuoteRuneToGraphic:")
fmt.Println("Orignal value before AppendQuoteRuneToGraphic()", string(x))
fmt.Println()
// Append a character (rune 108 is the Unicode of l)
x = strconv.AppendQuoteRuneToGraphic(x, 108)
fmt.Println("The new Value after AppendQuoteRuneToGraphic()", string(x))
fmt.Println()
// Append emoji
x = strconv.AppendQuoteRuneToGraphic(x, '🙉')
fmt.Println("The new Value after AppendQuoteRuneToGraphic()", string(x))
fmt.Println()
// Append emoji
x = strconv.AppendQuoteRuneToGraphic(x, '🙊')
// Append a character (rune 95 is the Unicode of _)
x = strconv.AppendQuoteRuneToGraphic(x, 95)
fmt.Println("The new Value after AppendQuoteRuneToGraphic()", string(x))
}

Explanation

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

Note: All the values assigned to the variable x are appended to the dst of type []byte.

Free Resources