Go print functions
last modified April 11, 2024
In this article we show how to print to console with fmt.Print, fmt.Printf, and fmt.Println.
The functions are variadic, i.e. they can accept multiple arguments.
$ go version go version go1.22.2 linux/amd64
We use Go version 1.22.2.
Go fmt.Print
The fmt.Print
writes its arguments to the standard output. It adds
spaces between operands when neither is a string.
func Print(a ...any) (n int, err error)
The function returns the number of bytes written and any write error encountered.
package main import "fmt" func main() { var w1, w2, w3 string = "An", "old", "falcon" fmt.Print("An old falcon\n") fmt.Print("An", " old", " falcon", "\n") fmt.Print(w1, " ", w2, " ", w3, "\n") var n1 int = 14 var n2 int = 11 var n3 int = 12 fmt.Print(n1, n2, n3) fmt.Print("\n") }
We write three messages and three integers to the console.
var w1, w2, w3 string = "An", "old", "falcon" fmt.Print("An old falcon\n") fmt.Print("An", " old", " falcon", "\n") fmt.Print(w1, " ", w2, " ", w3, "\n")
Three strings are written to the console. We explicitly add spaces and newlines.
var n1 int = 14 var n2 int = 11 var n3 int = 12 fmt.Print(n1, n2, n3)
When printing integers, the spaces are automatically added between them.
$ go run main.go An old falcon An old falcon An old falcon 14 11 12
Go fmt.Println
The fmt.Println
writes its arguments to the standard output and
appends a newline character. It adds spaces between operands when neither is a
string.
func Println(a ...any) (n int, err error)
The function returns the number of bytes written and any write error encountered.
package main import "fmt" func main() { var w1, w2, w3 string = "An", "old", "falcon" var n1 int = 14 var n2 int = 11 var n3 int = 12 fmt.Println("An old falcon") fmt.Println("An", " old", " falcon") fmt.Println(w1, " ", w2, " ", w3) fmt.Println(n1, n2, n3) }
In the example, we can remove newline characters since the
fmt.Println
automatically appends them.
Go fmt.Printf
The fmt.Printf
formats according to a format specifier and writes
to standard output.
func Printf(format string, a ...any) (n int, err error)
The function returns the number of bytes written and any write error encountered.
package main import ( "fmt" ) func main() { name := "Jane" age := 17 fmt.Printf("%s is %d years old\n", name, age) }
In the example, we write a formatted message to the console.
fmt.Printf("%s is %d years old\n", name, age)
The %s
specifier is used for strings and %d
for
integers.
$ go run main.go Jane is 17 years old
Go built-in print functions
Go also contains built-in print
and println
functions,
which are outside of any module. The functions are useful for quick debugging.
package main func main() { print("An old falcon\n") println("An old falcon") }
The documentation writes that they may be removed in the future.
Source
In this article we have showed how to print to the console in Go using
fmt.Print
, fmt.Println
, and fmt.Printf
functions.
Author
List all Go tutorials.