typestar

Los verbos de fmt en Go

El verbo decide cómo se muestra: valor, citado, tipo o sintaxis de Go.

type Run struct {
    Lang string
    TPM  float64
}

func verbs() []string {
    run := Run{Lang: "go", TPM: 104.567}
    return []string{
        fmt.Sprintf("%v", run),
        fmt.Sprintf("%+v", run),
        fmt.Sprintf("%#v", run),
        fmt.Sprintf("%T", run),
        fmt.Sprintf("%q|%8.2f|%-8s|", run.Lang, run.TPM, run.Lang),
        fmt.Sprintf("%d %05d %x %b", 42, 42, 255, 5),
    }
}

Cómo funciona

  1. %v es el predeterminado; %+v agrega los nombres de campo del struct.
  2. %q entrecomilla, %T imprime el tipo, %#v imprime sintaxis de Go.
  3. El ancho y la precisión van entre el porcentaje y el verbo.

Palabras clave y builtins usados aquí

El intento, en números

Líneas
16
Caracteres a escribir
331
Tokens
106
Ritmo de tres estrellas
95 tpm

Al ritmo de tres estrellas de 95 tokens por minuto, este intento toma unos 67 segundos.

Escribe este fragmento

Paso 4 de 5 en Cadenas y formato; paso 28 de 30 en Fundamentos del lenguaje.

← Anterior Siguiente →