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

The Go strconv.AppendQuoteRune() function

The strconv package’s AppendQuoteRune() function is used to append the single-quoted Go character literal, representing r (as generated by the QuoteRune() function), to dst and returns the extended buffer. The parameter dstis of type []byte and r is of type rune.

Syntax

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

Parameters

  • dst: 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 AppendQuoteRune() returns the extended buffer after appending the single-quoted character.

Example

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

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

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 AppendQuoteRune() 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 of type []byte.

Free Resources