What is the Print function in Golang?

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

The function prototype is shown below.

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

Functionality

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

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

The functionality of Print in Golang is similar to cout in C++ or printf in C.

Note: The Print() function does not append a new line at the end of the input data stream. However, it can be added explicitly by using '\n'. Space is only added between the arguments if none of the operands is a string.

The Print() function

Parameters and return

Print() 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 following:

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

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

Code

package main
import "fmt"
func main() {
fmt.Print("Hello", "world!\n") //no space b/w strings
name := "Ben"
age := 10
fmt.Print(name, age) // no space appended as 1 arg is a string
fmt.Println() // append new line
fmt.Print(0, 2, 4, 6) // space appended b/w int
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved