liveBook · Manning

Fmt.Println

As explained in Go in Action, Second Edition

fmt.Println is a Go standard-library function that prints its arguments with spaces between them and a trailing newline.

fmt.Println is the direct Go standard-library function for writing arguments to the screen. It prints arguments in order, places spaces between them, and ends the output with a newline. section 2.4.11 section 10.1

The package-qualified call follows Go's <package>.<function> syntax. The uppercase P in Println marks the function as exported, so code importing fmt can access it. section 2.4.11

Reporting a word count

After processing text, fmt.Println("Found", numSpaces+1, "words") produces a tidy result such as Found 6 words. Multiple arguments make it useful for simple status messages without manually joining values. section 2.4.11

Choose the related fmt function according to the output job:

  • Use fmt.Println for separate lines and automatic spacing between arguments. section 10.1
  • Use fmt.Print when output should not automatically end with a newline, such as an input prompt. section 10.1
  • Use fmt.Printf when a preformatted string and conversion verbs such as %s or %d provide more control. section 10.1
  • Use fmt.Sprint or fmt.Sprintf when the formatted result must become a string before later processing. section 10.1

CLI output and errors

A command-line tool can print a received value with fmt.Printf("Received: %s\n", input), then display a formatted validation error with fmt.Println(err). section 10.1

Listing 2.15 Using fmt.Println to output the results
fmt.Println("Found", numSpaces+1, "words")

Inspect the compact word-count example showing fmt.Println with text, a computed integer, and a final label. section 2.4.11

How Go in Action, Second Edition covers it

How deep it goes
Explains package qualification, exported naming, argument spacing, and newline behavior, then contrasts Println with related fmt functions.
Examples
Uses a word-count result, a JSON/XML CLI name, received input, and formatted validation errors.
What it assumes
Assumes basic Go packages and function calls while teaching package qualification and exported identifiers through fmt.Println.
What you can do after
Apply fmt.Println to emit spaced, newline-terminated messages and choose Print, Printf, or Sprint for different output requirements.
How it connects
Connects fmt.Println with the fmt package, package-qualified calls, exported identifiers, Print, Printf, Sprint, and Sprintf.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Action, Second Edition. The section links point to the parts of the book it draws on.