Skip to main content
In this article, you’ll learn various methods to print variables in Go (Golang). We will cover printing plain strings, variables, mixing strings and variables, handling newlines, and formatted printing using the fmt package. These techniques lay the foundation for effectively displaying data in your Go applications.

Printing a String

Begin by declaring a package, importing the fmt package, and using its Print function to output a string. For instance, to print “Hello World”, use the following code:
Run the program using:
The output will be:

Printing a Variable

To print a variable, declare and initialize it, then pass it to fmt.Print. For example, the code below declares a variable named city with a value of "Kolkata" and prints it:
Running this code displays the variable’s value.

Printing Variables and Strings Together

Often, you need to combine static text with variable values. Here’s an example where two variables, name and user, are printed alongside string literals:
Executing the program results in:
Remember that the fmt.Print function does not append a newline by default. When printing multiple items consecutively, they will appear without any line breaks.
For example, printing two variables without a newline:
Produces this output:

Adding Newlines with “\n”

To create line breaks, include the newline character (\n) in your string expressions. The following example demonstrates how to print variables on separate lines:
The output will be:
The newline character (\n) is interpreted as a line break rather than printed literally.

Automatic Newlines with fmt.Println

For convenience, the fmt package offers the Println function, which appends a newline automatically after printing each argument:
Running this code produces:

Formatted Printing Using fmt.Printf

When managing multiple arguments, formatted printing can simplify your code. The fmt.Printf function lets you embed format specifiers within a template string to control the output of each variable.

What is String Formatting?

String formatting allows you to integrate variables within a predefined text template. For example:
Here, the %s specifier indicates where a string variable should be inserted.

Common Format Specifiers

Below are some frequently used format specifiers in Go:

Example: Printing a String with %v

The output will be:

Example: Printing an Integer with %d

This code produces:

Combining Format Specifiers

You can combine multiple format specifiers in a single Printf call. For instance, the following example integrates a string and an integer:
The output appears as:

Summary of Format Specifiers

Below is an image that summarizes commonly used fmt.Printf format specifiers in Go:
The image is a table listing printf format specifiers, detailing verbs and their descriptions for formatting data types in programming.
That concludes our guide on printing variables in Go. In the next lesson, we will delve into more advanced topics to further enhance your Go programming skills.

Watch Video