Skip to main content
Module 1 · Lesson 5 of 55/50 in the course~10 min
Module lessons (5/5)

Input/output with fmt

Hardening Stage

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

Go
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

VerbMeaningExample output
%vDefault value for any type{Ada 36}
%+vValue with field names in structs{Name:Ada Age:36}
%#vComplete Go syntax (useful in testing/debugging)main.User{Name:"Ada", Age:36}
%TGo type the valuefloat64
%d / %b / %xInteger decimal / binary / hexadecimal42 / 101010 / 2a
%f / %.2fFloat / float to 2 decimal places3.140000 / 3.14
%s / %qEscaped plain/quoted stringAda / "Ada"
%tBoolean (true/false)true
Go
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):

Go
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:

Go
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

Exercise#go.m1.l5.e1
Attempts: 0Loading…

Print the string "ciao mondo" using fmt.Println.

Loading editor…
Show hint

Pass the string literal "ciao mondo" to fmt.Println.

Solution available after 3 attempts

Exercise#go.m1.l5.e2
Attempts: 0Loading…

Use fmt.Printf to print eta=36 followed by newline, formatting the variable age with the verb %d.

Loading editor…
Show hint

Printf requires both the %d placeholder and the \n ending sequence.

Solution available after 3 attempts

Exercise#go.m1.l5.e4
Attempts: 0Loading…

Construct the string "Ada: 95 pt" in summary using fmt.Sprintf with %s and %d, then print it with fmt.Println(summary).

Loading editor…
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

Quiz#go.m1.l5.e3
Ready

Which verb of fmt.Printf prints the Go TYPE of a value?

Go
fmt.Printf("?\n", 3.14)
Answer options
Quiz#go.m1.l5.e5
Ready

Which verb ALSO shows the field names when you print a User{Name: "Ada", Age: 36} struct?

Go
type User struct {
	Name string
	Age  int
}
u := User{Name: "Ada", Age: 36}
fmt.Printf("?\n", u)
Answer options