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.
fmt.Println("total:", 3, true) // total: 3 true
Try it
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.
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
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 toos.Stdout; usually ignored, andnilin 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() stringmethod prints that text; otherwise aString() stringmethod is used.
Usage notes
- Use
Printlnfor quick output and debugging. For logs uselogorlog/slog, which add time and severity. Printdiffers: no newline, and spaces only between operands when neither is a string.Println("a", "b")printsa b;Print("a", "b")printsab.- Pointers to structs, arrays, slices and maps print as
&{...}at the top level, but nested pointers print as addresses. - A
nilslice prints[], a nil mapmap[], and a nil error or interface<nil>. - Byte slices print as numbers (
[104 105]); usestring(b)or%s. Println(x)with a pointer receiverString()only uses it whenxis a pointer.- Standard output writes are not synchronized with
stderr, and concurrentPrintlncalls from goroutines each write whole lines, but the order between goroutines is not defined. - The built-in
printlnwrites to standard error and is meant for bootstrapping only; do not use it in programs. - Passing a slice such as
[]stringas the variadic argument without...prints it as one value; spreading requires[]any.
Related
- fmt, fmt.Printf(), fmt.Sprintf(), fmt.Fprintf()
- fmt.Stringer
- Tutorial: Output, Variadic functions