📌  相关文章
📜  Golang 中的 strconv.AppendQuote()函数示例

📅  最后修改于: 2021-10-25 03:05:58             🧑  作者: Mango

Go 语言提供内置支持,以通过strconv Package实现基本数据类型的字符串表示的转换。这个包提供了一个AppendQuote()函数,该函数将表示由 Quote 生成的 str 的双引号 Go字符串字面量附加到 num 并返回扩展缓冲区,如下面的语法所示。要访问 AppendQuote()函数,您需要在程序中导入strconv包。

句法:

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

示例 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))
  
}

输出:

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

示例 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))
  
}

输出:

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