How to use the strconv.AppendQuoteToAscii() in Go?
Overview
The strconv package’s AppendQuoteToASCII() function is used to append the double-quoted Go string literal, representing s (as generated by QuoteToASCII() function), to dst and returns the extended buffer.
Syntax
func AppendQuoteToASCII(dst []byte, s string) []byte
Parameters
dst: is a byte array to which the double-quoted character is appended literally.s: is the string character to be appended.
Return value
The function AppendQuoteToASCII() returns the extended buffer after appending the double-quoted string.
Code example
The following code shows how to implement the AppendQuoteToASCII() function in Go.
// Using strconv.AppendQuoteToASCII() Functionpackage mainimport ("fmt""strconv")func main() {x := []byte("Empty")fmt.Println("initial value in the array before Appending", string(x))fmt.Println()// Append a stringx = strconv.AppendQuoteToASCII(x, `"Keep learning! Keep Coding!"`)fmt.Println("The new value after Appending", string(x))fmt.Println()// Append a stringx = strconv.AppendQuoteToASCII(x, `"Golang is one of my favourite languages"`)fmt.Println("The new values", string(x))fmt.Println()// Append a stringx = strconv.AppendQuoteToASCII(x, `"Educative Shot"`)x = strconv.AppendQuoteToASCII(x, `"Go Programming Language"` )fmt.Println("The new value in the array", string(x))}
Code 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 variablexof type string, assign a value to it, and pass the variable in theAppendQuoteToASCII()function. This returns a double-quoted Go string literal representing the given string. - Finally, we display the result.
Note: All the values assigned to variable x are appended to the dst.