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.
func AppendQuoteRune(dst []byte, r rune) []byte
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.The function AppendQuote()
returns the extended buffer after appending the double-quoted string.
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))}
Line 4: We add the main
package.
Lines 6–9: We import the necessary packages.
Lines 11–25: We define the main()
function and the variables x
and y
of 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.