What is the Println function in Golang?
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.
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 emptyinterfaceto 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:intvalue to represent the total number of characters written to the standard output. -
err: variable of typeerrorin case we are dealt with an error during execution of the function.
Code
package mainimport "fmt"func main() {fmt.Println("Hello", "world!")name := "Bob"height := 156fmt.Println("Name:", name)fmt.Println("Height:", height, "cm")}
Free Resources