Función fmt.Sprint() en Golang con ejemplos

En el lenguaje Go, el paquete fmt implementa E/S formateadas con funciones análogas a las funciones printf() y scanf() de C. La función fmt.Sprint() en los formatos de idioma Go utiliza los formatos predeterminados para sus operandos y devuelve la string resultante. Aquí se agregan espacios entre operandos cuando no se usa ninguna string como variable constante. Además, esta función está definida en el paquete fmt. Aquí, debe importar el paquete «fmt» para usar estas funciones.

Sintaxis:

func Sprint(a ...interface{}) string

Aquí, “una …interfaz{}” contiene algunas strings que incluyen variables constantes especificadas.

Devoluciones: Devuelve la string resultante.

Ejemplo 1:

// Golang program to illustrate the usage of
// fmt.Sprint() function
  
// Including the main package
package main
  
// Importing fmt, io and os
import (
    "fmt"
    "io"
    "os"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const name, dept = "GeeksforGeeks", "CS"
  
    // Calling Sprint() function
    s := fmt.Sprint(name, " is a ", dept, " Portal.\n")
  
    // Calling WriteString() function to write the
    // contents of the string "s" to "os.Stdout"
    io.WriteString(os.Stdout, s)
  
}

Producción:

GeeksforGeeks is a CS Portal.

Ejemplo 2:

// Golang program to illustrate the usage of
// fmt.Sprint() function
  
// Including the main package
package main
  
// Importing fmt, io and os
import (
    "fmt"
    "io"
    "os"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const num1, num2, num3 = 5, 10, 15
  
    // Calling Sprint() function
    s := fmt.Sprint(num1, num2, num3)
  
    // Calling WriteString() function to write the
    // contents of the string "s" to "os.Stdout"
    io.WriteString(os.Stdout, s)
  
}

Producción:

5 10 15

En el código anterior, se puede ver que la función Sprint() no usa ningún espacio; aún así, se puede ver espacio en la salida entre los números porque la función en sí agregó los espacios porque no se usa una sola string como variable constante.

Ejemplo 3:

// Golang program to illustrate the usage of
// fmt.Sprint() function
  
// Including the main package
package main
  
// Importing fmt, io and os
import (
    "fmt"
    "io"
    "os"
)
  
// Calling main
func main() {
  
    // Declaring some const variables
    const str1, str2, str3 = "a", "b", "c"
  
    // Calling Sprint() function
    s := fmt.Sprint(str1, str2, str3)
  
    // Calling WriteString() function to write the
    // contents of the string "s" to "os.Stdout"
    io.WriteString(os.Stdout, s)
  
}

Producción:

abc

En el código anterior, se puede ver que la función Sprint() no usa ningún espacio y tampoco se pueden ver espacios en la salida entre dos alfabetos, esto se debe a que las strings se usan en las variables constantes.

Publicación traducida automáticamente

Artículo escrito por Kanchan_Ray 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 *