How to use the strconv.AppendQuote() function in Golang
Golang strconv.AppendQuote()
The strconv package’s AppendQuote() method is used to append the double-quoted Go string literal representing s (as generated by the Quote() function) to dst and return the extended buffer.
Syntax
func AppendQuoteRune(dst []byte, r rune) []byte
Parameters
dst: This is a byte array to which the double-quoted string is to be appended.s: This is the double-quoted string to be appended.
Return value
The function AppendQuote() returns the extended buffer after appending the double-quoted string.
Code
The following code shows how to use the AppendQuote() function in Golang.
// Using strconv.AppendQuote() Functionpackage mainimport ("fmt""strconv")func main() {// Declaring and assigning valuex := []byte("Quote 1:")fmt.Println()fmt.Println("Before AppendQuote():", string(x))// Using AppendQuote to appending a quote to the xx = strconv.AppendQuote(x, `"Keep learning! keep coding!"`)fmt.Println()fmt.Println("After AppendQuote()", string(x))// Declaring and assigning valuey := []byte("Quote 2:")fmt.Println()fmt.Println("Before AppendQuote():", string(y))// Using AppendQuote to appending a quote to the xy = strconv.AppendQuote(y, `"Shot on strconv.AppendQuote()"`)fmt.Println()fmt.Println("After AppendQuote()", string(y))}
Code explanation
-
Line 4: We add the
mainpackage. -
Lines 6–9: We import the necessary packages.
-
Lines 11–25: We define the
main()function and the variablesxandyof byte array type, and assign a value to each. -
Lines 18-20: We pass the variable to the
AppendQuote()function, which appends the double-quoted Go string literal. -
Finally, the double-quoted string is displayed.