Print()
is defined in the fmt
package in Golang.
The function prototype is shown below.
func Print(a ...interface{}) (n int, err error)
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:
ThePrint()
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.
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.
package mainimport "fmt"func main() {fmt.Print("Hello", "world!\n") //no space b/w stringsname := "Ben"age := 10fmt.Print(name, age) // no space appended as 1 arg is a stringfmt.Println() // append new linefmt.Print(0, 2, 4, 6) // space appended b/w int}
Free Resources