Open In App

strconv.AppendQuote() 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 AppendQuote() function which appends a double-quoted Go string literal representing str, as generated by Quote, to num and returns the extended buffer as shown in below syntax. To access AppendQuote() function you need to import strconv Package in your program.

Syntax:



func AppendQuote(num []byte, str string) []byte

Example 1:




// Golang program to illustrate
// strconv.AppendQuote() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Using AppendQuote() function
    val1 := []byte("Result 1: ")
    val1 = strconv.AppendQuote(val1,
         `"Welcome GeeksforGeeks"`)
      
    fmt.Println(string(val1))
  
    val2 := []byte("Result 2: ")
    val2 = strconv.AppendQuote(val2,
                          `"Hello"`)
      
    fmt.Println(string(val2))
  
}

Output:



Result 1: "\"Welcome GeeksforGeeks\""
Result 2: "\"Hello\""

Example 2:




// Golang program to illustrate
// strconv.AppendQuote() Function
package main
  
import (
    "fmt"
    "strconv"
)
  
func main() {
  
    // Using AppendQuote() function
    val1 := []byte("String 1: ")
    val1 = strconv.AppendQuote(val1,
                  `"GeeksforGeeks"`)
      
    fmt.Println(string(val1))
    fmt.Println("Length: ", len(val1))
    fmt.Println("Capacity: ", cap(val1))
  
    val2 := []byte("String 2: ")
    val2 = strconv.AppendQuote(val2, 
            `"Hello! How are you?"`)
      
    fmt.Println(string(val2))
    fmt.Println("Length: ", len(val2))
    fmt.Println("Capacity: ", cap(val2))
  
}

Output:

String 1: "\"GeeksforGeeks\""
Length:  29
Capacity:  64
String 2: "\"Hello! How are you?\""
Length:  35
Capacity:  80

Article Tags :