Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท fmt Package

Go fmt.Println() function

Reference for Go fmt.Println(): print values to standard output separated by spaces with a trailing newline, how types are formatted, and its return values.

fmt.Println writes its arguments to standard output, separates them with a single space, and ends the line with a newline. It formats each argument the way %v would, so numbers, strings, slices, maps, structs and errors all print in a readable default form.

It is the quickest way to inspect a value while developing. For control over widths, precision and layout, use fmt.Printf.

Go
fmt.Println("total:", 3, true)
// total: 3 true

Try it

Go Default formats

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Return values and variadic arguments

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func Println(a ...any) (n int, err error)
Name Description
a Zero or more values of any type.

Returns

  • n: the number of bytes written (including the newline).
  • err: any error from writing to os.Stdout; usually ignored, and nil in practice for a terminal or pipe that stays open.

Behavior

  • Operands are always separated by spaces, even between strings.
  • With no arguments, only a newline is printed.
  • A value with an Error() string method prints that text; otherwise a String() string method is used.

Usage notes

  • Use Println for quick output and debugging. For logs use log or log/slog, which add time and severity.
  • Print differs: no newline, and spaces only between operands when neither is a string. Println("a", "b") prints a b; Print("a", "b") prints ab.
  • Pointers to structs, arrays, slices and maps print as &{...} at the top level, but nested pointers print as addresses.
  • A nil slice prints [], a nil map map[], and a nil error or interface <nil>.
  • Byte slices print as numbers ([104 105]); use string(b) or %s.
  • Println(x) with a pointer receiver String() only uses it when x is a pointer.
  • Standard output writes are not synchronized with stderr, and concurrent Println calls from goroutines each write whole lines, but the order between goroutines is not defined.
  • The built-in println writes to standard error and is meant for bootstrapping only; do not use it in programs.
  • Passing a slice such as []string as the variadic argument without ... prints it as one value; spreading requires []any.