Función strconv.AppendQuoteToASCII() en Golang con ejemplos

El lenguaje Go proporciona soporte incorporado para implementar conversiones hacia y desde representaciones de strings de tipos de datos básicos mediante el paquete strconv. Este paquete proporciona una función AppendQuoteToASCII() que se usa para agregar un literal de string Go entre comillas dobles que representa str, generado por QuoteToASCII, a num y devuelve el búfer extendido. O, en otras palabras, convierte la string str en una string ASCII resultante de «comillas dobles», agrega el resultado al final de num y devuelve el [] byte agregado. Para acceder a la función AppendQuoteToASCII(), debe importar el paquete strconv en su programa.

Sintaxis:

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

Aquí, num es []bytes y str es una string. El resultado de str se agregará al final de num.

Ejemplo 1:

// 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! GeeksforGeeks"`)
    fmt.Println(string(val1))
  
    val2 := []byte("Result 2: ")
    val2 = strconv.AppendQuoteToASCII(val2, `"Hey"`)
    fmt.Println(string(val2))
  
}

Producción:

Result 1: "\"Hello! GeeksforGeeks\""
Result 2: "\"Hey\""

Ejemplo 2:

// 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))
  
}

Producción:

Result 1: "\"Hello! GFG\""
Length:  26
Capacity:  48
Result 2: "\"Welcome\""
Length:  23
Capacity:  48

Publicación traducida automáticamente

Artículo escrito por ankita_saini y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *