Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the Println function in Golang?

Khizar Hayat Saani

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Println() is defined in the fmt package in Golang.

The following is the function prototype:

func Println(a ...interface{}) (n int, err error)

Functionality

We use Println() to write the input data stream to the standard output.

It is a variadic function, which means that it takes a variable number of input arguments.

The functionality of Println() in Golang is similar to print in python or printf in C.

Note: The Println() function appends a new line at the end of the input data stream. Each of the input arguments will automatically be separated by a space in the output.

The Println() function

Parameters and return

Println() is a variadic function. Each input argument type is an empty interface. This means we can pass any data type to the function ranging from (but not limited to) string, float, int, or struct.

The function takes the following input parameter:

  • a: generic reference to input argument(s). The type of the input argument(s) is an empty interface to accommodate all data types.

The function returns the total number of characters written to the standard output, or an error if we are dealt one.

The function returns the following:

  • n: int value to represent the total number of characters written to the standard output.

  • err: variable of type error in case we are dealt with an error during execution of the function.

Code

package main
import "fmt"
func main() {
fmt.Println("Hello", "world!")
name := "Bob"
height := 156
fmt.Println("Name:", name)
fmt.Println("Height:", height, "cm")
}

RELATED TAGS

golang

CONTRIBUTOR

Khizar Hayat Saani
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring