How to use the strconv.AppendQuoteToGraphic() function in Golang

Overview

The strconv package’s AppendQuoteToGraphic() function appends the double-quoted Go string literal representing s (as generated by QuoteToGraphic() function), to dst. It returns the extended buffer.

Syntax

func AppendQuoteToGraphic(dst []byte, s string) []byte

Parameters

  • dst: This is a byte array to which the double-quoted Go string literal is to be appended.
  • s: This is the string to be appended.

Return value

The AppendQuoteToGraphic() function returns the extended buffer after appending the double-quoted Go string literal.

Code

The following code shows how to implement the AppendQuoteToGraphic() function in Golang:

// Using strconv.AppendQuoteToGraphic() Function
package main
import (
"fmt"
"strconv"
)
func main() {
s := []byte("Empty:")
fmt.Println("Initial value before AppendQuoteToGraphic()", string(s))
fmt.Println()
// Append a string
s = strconv.AppendQuoteToGraphic(s, "Welcome \u2766 to another shot on Golang,")
fmt.Println("The new Value after AppendQuoteToGraphic()", string(s))
fmt.Println()
}

Explanation

  • Line 4: We add the main package.
  • Lines 6 to 9: We import the necessary packages in the Golang project.
  • Lines 11 to 21: We define the main() function. Within the main() function:
    • Line 12: We define the variable s of the string type.
    • Line 13: We use the print() function to display the initial value.
    • Line 18: We assign a value to the s variable, and pass the variable in the AppendQuoteToGraphic() function. This returns a double-quoted Go string literal representing value.
    • Line 19: Finally, we display the result.

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

Free Resources