Home > Article > Backend Development > Which Go Print Function Should You Use: Printf, Println, or Print?
Printf vs. Println vs. Print in Go
Go offers three versatile functions for outputting data to the console: Printf, Println, and Print. While these functions share the common goal of displaying information, they differ in their capabilities and formatting options.
Printf
Printf (a.k.a. "Print Formatter") is the most versatile and complex of the three functions. It allows you to format and insert values into a string using format specifiers such as "%s" (for strings), "%d" (for integers), and "%f" (for floats). For example, you could use Printf to print the type of a variable using the format specifier "%T":
fmt.Printf("%T", FirstName)
This would output "string", as FirstName is a string variable.
Print is the simplest of the three functions. It simply prints a list of arguments (strings, variables, etc.) to the console, with no formatting. For example:
fmt.Print("Hello", " ", "World!")
This would output "Hello World!" on the console.
Println
Println (a.k.a. "Print Line") is similar to Print, but with the addition of an automatic newline character ("n") at the end. This is useful for printing multiple statements on separate lines:
fmt.Println("Line 1") fmt.Println("Line 2")
This would output:
Line 1 Line 2
The above is the detailed content of Which Go Print Function Should You Use: Printf, Println, or Print?. For more information, please follow other related articles on the PHP Chinese website!