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
