Skip to main content

Go Output Functions

You can output text in Go with any of three functions:

  • Print()
  • Println()
  • Printf()

The Print() Function

Print() outputs its arguments using their default format.

Note: To start a new line, use \n.

When neither of two arguments is a string, Print() puts a space between them.

Example

Here, i and j are printed one after the other, with nothing between them:

Prompt
Use fmt.Print() to print two string variables without newlines.
Output: Go program and captured output

Example

To put each argument on its own line, add \n after it:

Prompt
Use fmt.Print() with the newline escape character to print on separate lines.
Output: Go program and captured output

Example

One Print() call can handle several variables at once:

Prompt
Use a single fmt.Print() call with multiple arguments including a newline.
Output: Go program and captured output

Example

Strings get no automatic space between them, so you have to add one yourself with " ":

Prompt
Use fmt.Print() with a space string between two arguments.
Output: Go program and captured output

Example

With integer arguments there are no strings involved, so Print() adds the space by itself:

Prompt
Show that fmt.Print() adds a space between non-string arguments.
Output: Go program and captured output

The Println() Function

Println() works much like Print(), with two additions: it always puts whitespace between its arguments, and it ends the output with a newline:

Example

Prompt
Use fmt.Println() to print two strings with automatic space and newline.
Output: Go program and captured output

The Printf() Function

Printf() formats its arguments according to the formatting verb you give it, and only then prints them.

This example uses two of those verbs:

  • %v prints an argument's value
  • %T prints an argument's type

Example

Prompt
Use fmt.Printf() with %v and %T verbs to print values and types.
Output: Go program and captured output