Module lessons (5/5)
Input/output with fmt
Formatting and diagnostics with fmt
Print complex structures, dynamic types and messages ready for real logs and errors.
The fmt package of the standard library governs console printing, string construction and error formatting. Compared to C's printf, Go introduces the universal verb %v capable of inspecting any type, including structs and slices.
Print, Println and Printf compared
package main
import "fmt"
func main() {
fmt.Print("ciao ", "mondo\n") // nessun separatore né newline automatica
fmt.Println("ciao", "mondo") // aggiunge spazio tra argomenti e \n finale
fmt.Printf("age=%d\n", 36) // controllato dalla format string esplicita
}The essential format verbs
| Verb | Meaning | Example output |
|---|---|---|
%v | Default value for any type | {Ada 36} |
%+v | Value with field names in structs | {Name:Ada Age:36} |
%#v | Complete Go syntax (useful in testing/debugging) | main.User{Name:"Ada", Age:36} |
%T | Go type the value | float64 |
%d / %b / %x | Integer decimal / binary / hexadecimal | 42 / 101010 / 2a |
%f / %.2f | Float / float to 2 decimal places | 3.140000 / 3.14 |
%s / %q | Escaped plain/quoted string | Ada / "Ada" |
%t | Boolean (true/false) | true |
package main
import "fmt"
type User struct {
Name string
Age int
}
func main() {
u := User{Name: "Ada", Age: 36}
fmt.Printf("v=%v\n+v=%+v\n#v=%#v\nT=%T\n", u, u, u, u)
}Sprintf and Errorf without direct printing
fmt.Sprintf applies the same rules as Printf returning a string, while fmt.Errorf returns a error (and with %w encapsulates the original error):
package main
import (
"errors"
"fmt"
)
func loadConfig(path string) error {
baseErr := errors.New("file missing")
return fmt.Errorf("config %q: %w", path, baseErr)
}
func main() {
line := fmt.Sprintf("User %s: %.2f%%", "Ada", 99.5)
fmt.Println(line)
fmt.Println(loadConfig("/etc/app.yaml"))
}Write to os.Stderr and format with Fprintf
To distinguish results from diagnostic messages, the Fprint* family directs output to any io.Writer, such as os.Stderr or a bytes.Buffer:
package main
import (
"fmt"
"os"
)
func main() {
code := 7
fmt.Fprintf(os.Stderr, "[WARN %04d] connessione lenta (latenza=%.1fms)\n", code, 142.37)
}Guided practice
Print the string "ciao mondo" using fmt.Println.
Show hint
Pass the string literal "ciao mondo" to fmt.Println.
Solution available after 3 attempts
Use fmt.Printf to print eta=36 followed by newline, formatting the variable age with the verb %d.
Show hint
Printf requires both the %d placeholder and the \n ending sequence.
Solution available after 3 attempts
Construct the string "Ada: 95 pt" in summary using fmt.Sprintf with %s and %d, then print it with fmt.Println(summary).
Show hint
fmt.Sprintf("%s: %d pt", name, points) returns a string without printing anything until you call fmt.Println(summary).
Solution available after 3 attempts
Check for understanding
Which verb of fmt.Printf prints the Go TYPE of a value?
fmt.Printf("?\n", 3.14)Which verb ALSO shows the field names when you print a User{Name: "Ada", Age: 36} struct?
type User struct {
Name string
Age int
}
u := User{Name: "Ada", Age: 36}
fmt.Printf("?\n", u)