Go Output Functions
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Go Output Functions

Kishore V

Go Output Functions

In Go, output is handled using functions from the built-in fmt package. The three most commonly used functions are:

  • fmt.Print()
  • fmt.Println()
  • fmt.Printf()

Each serves a different purpose depending on how you want to format and display output.

fmt.Print() – Basic Output

The Print() function outputs values using their default format. It does not automatically add spaces between strings or a newline at the end.

Example:

GoodMorning

Adding Line Breaks with \n

To move output to a new line, use the newline escape sequence \n.

Welcome To Go Programming

Printing Multiple Values

You can pass multiple arguments to Print(). However, spacing depends on the data types.

Name:Anita Age:22

Note: When printing non-string values together, Go automatically inserts spaces.

fmt.Println() – Line-Based Output

The Println() function is similar to Print(), but with two key differences:

  • Automatically inserts spaces between arguments
  • Adds a newline at the end
City: Chennai Temperature: 32

fmt.Printf() – Formatted Output

The Printf() function allows you to format output using format specifiers (verbs). It gives you precise control over how values are displayed.

Common Format Verbs:

Verb Description
%v Value (default format)
%T Type of the value

Product: Laptop (Type: string) Price: 59999.99 (Type: float64)

When to Use Each Function

  • Use Print() for simple output without formatting
  • Use Println() when you want automatic spacing and line breaks
  • Use Printf() for structured and formatted output

Go Printf() Formatting Verbs

In Go, the fmt.Printf() function provides powerful formatting capabilities using formatting verbs. These verbs control how values are displayed in the output, making your programs more readable and professional.

What Are Formatting Verbs?

Formatting verbs are placeholders (starting with %) used inside a format string. They are replaced by variable values when the program runs.

Go was released in 2009

General Formatting Verbs

Verb Description
%v Default value format
%#v Go-syntax representation
%T Type of the value
%% Prints a literal %

Value: 42.75 Go-syntax: 42.75 Type: float64 Completion: 85% Text: GoLang | "GoLang" | string

Integer Formatting Verbs

These verbs are used specifically for integer values.

Verb Description
%bBinary (base 2)
%dDecimal (base 10)
%+dDecimal with sign
%oOctal
%OOctal with 0o prefix
%xHexadecimal (lowercase)
%XHexadecimal (uppercase)
%#xHex with 0x prefix
%4dWidth 4 (right-aligned)
%-4dWidth 4 (left-aligned)
%04dZero-padded width 4


String Formatting Verbs

These verbs control how strings are displayed.

Verb Description
%sPlain string
%qDouble-quoted string
%8sRight-aligned (width 8)
%-8sLeft-aligned (width 8)
%xHex representation
% xHex with spaces


Boolean Formatting Verb

Verb Description
%t Boolean value (true or false)

Logged in: true Admin: false

Float Formatting Verbs

These verbs format floating-point numbers.

Verb Description
%eScientific notation
%fDecimal format
%.2fPrecision (2 decimal places)
%6.2fWidth 6, precision 2
%gCompact format

Scientific: 3.141590e+00 Decimal: 3.141590 Rounded: 3.14 Width + precision: 3.14 Compact: 3.14159


Our website uses cookies to enhance your experience. Learn More
Accept !