Menu
Coddy logo textTech

Formatted Output

Part of the Fundamentals section of Coddy's GO journey — lesson 29 of 109.

Let's learn how to format our output in Go using the fmt.Printf() function. This function allows us to display values in specific formats using placeholders.

First, let's create a program that formats different data types using placeholders:

name := "Gopher"
age := 5
height := 1.5
    
fmt.Printf("Name: %s, Age: %d, Height: %.1f\n", name, age, height)

After executing this code, the text "Name: Gopher, Age: 5, Height: 1.5" will show on the screen. The placeholders in our string are replaced with the actual values of our variables.

Let's understand what each placeholder does:

// %s is replaced with a string value (name)
// %d is replaced with an integer value (age)
// %.1f is replaced with a float value showing 1 decimal place (height)
// %t is replaced with a boolean value (true or false)

The fmt.Printf() function replaces each placeholder with the corresponding variable that we provide after the format string. The order of variables must match the order of placeholders.

Now let's try another example with different types of formatting:

pi := 3.14159
percentage := 0.75
letter := 'A'

fmt.Printf("Pi: %.2f, Percentage: %.1f%%, Letter: %c\n", pi, percentage*100, letter)

After executing this code, the text "Pi: 3.14, Percentage: 75.0%, Letter: A" will show on the screen. Notice how %.2f shows only 2 decimal places for pi, and we multiply the percentage by 100 to show it as a percentage value.

Also notice the special sequence %% in the format string — this is how you print a literal % character with fmt.Printf(). A single % would be treated as the start of a placeholder, so %% is used to escape it and display the percent sign itself.

challenge icon

Challenge

Easy

In this challenge, you'll practice using formatted output in Go with the `fmt.Printf()` function.

You have information about a product that needs to be displayed in a specific format using format verbs. The variables are already defined for you.

Your task is to use `fmt.Printf()` with the appropriate format verbs to display the product information:
- Use `%s` for strings
- Use `%d` for integers  
- Use `%.2f` for floating-point numbers with 2 decimal places
- Use `%t` for boolean values

Display the product information exactly as shown in the expected output.

Cheat sheet

Use fmt.Printf() to format output with placeholders:

fmt.Printf("Name: %s, Age: %d, Height: %.1f\n", name, age, height)

Common format verbs:

  • %s - string value
  • %d - integer value
  • %.1f - float with 1 decimal place
  • %.2f - float with 2 decimal places
  • %t - boolean value
  • %c - character value
  • %% - literal percent sign (%)

The order of variables must match the order of placeholders in the format string.

Use \n at the end of the format string to move to a new line after printing.

Try it yourself

package main

import "fmt"

func main() {
	// Product information
	productName := "Wireless Headphones"
	productPrice := 79.99
	productInStock := true
	productID := 12345
	
    // TODO: Use fmt.Printf() with format verbs to display the product information:
    // Product: Wireless Headphones (ID: 12345)
    // Price: $79.99
    // In Stock: true
    //
    // Hint: Use %s for strings, %d for integers, %.2f for floats, %t for booleans
    // Remember to add \n at the end of each line for proper formatting
	
	
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals