strconv.AppendQuoteToASCII() Function in Golang With Examples
Go language provides inbuilt support to implement conversions to and from string representations of basic data types by strconv Package. This package provides an
AppendQuoteToASCII() function
which is used to append a double-quoted Go string literal representing str, as generated by QuoteToASCII, to num and returns the extended buffer. Or in other words, converts the string str to an ASCII string resulting from “double quotes”, append the result to the end of num and return the appended []byte. To access AppendQuoteToASCII() function you need to import strconv Package in your program.
Syntax:
func AppendQuoteToASCII(num []byte, str string) []byte
Here, num is []bytes and str is a string. The result of str will append to the end of num.
Example 1:
C
// Golang program to illustrate the // strconv.AppendQuoteToASCII() function package main import ( "fmt" "strconv" ) func main() { // Converting the string to ASCII // strings resulting from "single quotes" // append the result to the // end of the given []byte // Using AppendQuoteToASCII() function val1 := []byte("Result 1: ") val1 = strconv.AppendQuoteToASCII(val1, `"Hello! w3wiki"`) fmt.Println(string(val1)) val2 := []byte("Result 2: ") val2 = strconv.AppendQuoteToASCII(val2, `"Hey"`) fmt.Println(string(val2)) } |
Output:
Result 1: "\"Hello! w3wiki\""
Result 2: "\"Hey\""
Example 2:
C
// Golang program to illustrate the // strconv.AppendQuoteToASCII() function package main import ( "fmt" "strconv" ) func main() { // Converting the string to ASCII // strings resulting from "single quotes" // append the result to the // end of the given []byte // Using AppendQuoteToASCII() function val1 := []byte("Result 1: ") val1 = strconv.AppendQuoteToASCII(val1, `"Hello! GFG"`) fmt.Println(string(val1)) fmt.Println("Length: ", len(val1)) fmt.Println("Capacity: ", cap(val1)) val2 := []byte("Result 2: ") val2 = strconv.AppendQuoteToASCII(val2, `"Welcome"`) fmt.Println(string(val2)) fmt.Println("Length: ", len(val2)) fmt.Println("Capacity: ", cap(val2)) } |
Output:
Result 1: "\"Hello! GFG\""
Length: 26
Capacity: 48
Result 2: "\"Welcome\""
Length: 23
Capacity: 48
Contact Us