0Pricing
Go Academy · Lesson

Printf Verbs

Format values precisely.

Printf Verbs is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Verb?

fmt.Printf formats values using a format string. Inside it, a percent sign plus a letter is a verb that says how to print the next argument.

Example: %d for integers, %s for strings.

package main

import "fmt"

func main() {
    fmt.Printf("%s is %d years old\n", "Ada", 36)
}

Remember the Newline

Unlike Println, Printf does not add a line break. Add \n yourself when you want one.

package main

import "fmt"

func main() {
    fmt.Printf("line one\n")
    fmt.Printf("line two\n")
}

The Universal Verb %v

%v prints a value in its default format and works for almost any type. %+v adds struct field names.

package main

import "fmt"

type Point struct{ X, Y int }

func main() {
    p := Point{1, 2}
    fmt.Printf("%v\n", p)
    fmt.Printf("%+v\n", p)
}

The Go-Syntax Verb %#v

%#v prints a value the way you would write it in Go source code. It is great for debugging.

package main

import "fmt"

type Point struct{ X, Y int }

func main() {
    p := Point{1, 2}
    fmt.Printf("%#v\n", p)
}

The Type Verb %T

%T prints the type of a value, which is helpful when you are unsure what you are working with.

package main

import "fmt"

func main() {
    fmt.Printf("%T\n", 42)
    fmt.Printf("%T\n", "hi")
    fmt.Printf("%T\n", 3.14)
}

Integer Bases

Integers have several verbs:

  • %d decimal,
  • %b binary,
  • %o octal,
  • %x hexadecimal.
package main

import "fmt"

func main() {
    n := 255
    fmt.Printf("%d %b %o %x\n", n, n, n, n)
}

Floating-Point Format

Floats use %f for decimal and %e for scientific notation. %g picks the shortest sensible form.

package main

import "fmt"

func main() {
    x := 3.14159
    fmt.Printf("%f\n", x)
    fmt.Printf("%e\n", x)
    fmt.Printf("%g\n", x)
}

Precision for Floats

Add .N to control decimal places. %.2f shows exactly two digits after the point, perfect for money.

package main

import "fmt"

func main() {
    price := 9.5
    fmt.Printf("$%.2f\n", price)
}

Width and Alignment

A number before the verb sets a minimum width. Values are right-aligned by default; a minus sign left-aligns them.

package main

import "fmt"

func main() {
    fmt.Printf("[%5d]\n", 42)
    fmt.Printf("[%-5d]\n", 42)
}

Padding with Zeros

A leading 0 in the width pads numbers with zeros, ideal for fixed-length codes or timestamps.

package main

import "fmt"

func main() {
    fmt.Printf("%04d\n", 7)
    fmt.Printf("%02d:%02d\n", 9, 5)
}

Booleans, Quotes, and Percent

More verbs: %t for booleans, %q for a double-quoted string, and %% for a literal percent sign.

package main

import "fmt"

func main() {
    fmt.Printf("%t\n", true)
    fmt.Printf("%q\n", "hi")
    fmt.Printf("100%%\n")
}

Quick Check

Which verb formats a float with two decimals?

Recap: Printf Verbs

You can now format precisely:

  • General: %v, %+v, %#v, %T.
  • Numbers: %d, %b, %x, %f, %.2f.
  • Layout: width, - for left align, 0 for zero padding.
package main

import "fmt"

func main() {
    fmt.Printf("%-8s %6.2f\n", "Total:", 12.5)
}

Frequently asked questions

Is the “Printf Verbs” lesson free?

Yes — the full text of “Printf Verbs” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Printf Verbs”?

Format values precisely. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Printf Verbs” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Printf Verbs
  2. Sprintf and Fprintf
  3. The Stringer Interface
  4. Scanning Input
← Back to Go Academy