Go Ouput Funtions

In Go, there are three primary functions for printing output: Print(), Println(), and Printf(). These functions allow you to display data and messages in the console with varying levels of formatting.

Tutorials dojo strip



The Print() Function

The Print() function outputs its arguments using their default format without adding spaces or newlines.

Example:

This program demonstrates printing two strings side by side:

Go

Result:

Go



Example:

To print each argument on a new line, you can use \n:

Go

Result:

Go



Example:

Alternatively, one Print() can be used to display multiple variables:

Go

Result:

Go



Example:

To add a space between string arguments, include a space explicitly:

Go

Result:

Go



Example:

If the arguments are not strings, Print() automatically inserts a space:

Go

Result:

Go




The Println() Function

The Println() function is similar to Print() but automatically adds a space between its arguments and appends a newline at the end.

Example:

Go

Result:

Go




The Printf() Function

The Printf() function allows for advanced formatting of its arguments. It formats the input according to specific “verbs.”

Common Formatting Verbs:

  • %v: Displays the value of the argument.
  • %T: Displays the type of the argument.

Example:

Go

Result:

Go

Tutorials dojo strip