The Println() function in Golang

Printing data to the console is an essential task that not only helps debug the code but makes us write good code. In this post, we are going to take a look at the Println() function in Golang, which is a variadic function.

Golang Println() Method Signature

The method signature of the Println function is shown below.

fmt.Println(...interface{})

It means it takes a variable number of arguments, which can be of any type.

It formats the type with their default string format and it will output the printed version in the console.

It inserts a newline after printing.

Whitespace insertion in Println() function

When multiple values are given in the function, the function automatically inserts whitespace between those values.

package main

import (
	"fmt"
)

func main() {
	fmt.Println("Hello,", "world") //Hello, world
}

Printing values using Println() function

Any value that is given in this function will be printed by their default format of printing.

fmt.Println(42)       // 42
fmt.Println(123.456)  // 123.456

Println function Benefits

This function has several benefits over others. It automatically formats all the specified types. Adds a newline after printing. And can take any number of arguments and will print them serially. This is pretty useful when debugging. This can save time when we want to print the value directly without knowing any format specifier at all.

Further Reading: Golang sleep function