Go Console Output Functions: Println, Print, and Printf
Print, Println, and Printf
fmt.Print() prints its arguments as strings.
fmt.Println() inserts spaces between arguments and prints a newline character (\n) at the end.
fmt.Printf() prints arguments using format specifiers such as %d for numbers and %s for strings.
package main
import "fmt"
func main() {
num := 123
str := "ABC"
fmt.Print("num=", num, " str=", str, "\n") // no automatic newline, spaces, or formatting
fmt.Println("num =", num, "str =", str ) // automatic newline and spaces, no formatting
fmt.Printf("num=%d str=%s\n", num, str) // no automatic newline or spaces, with formatting
}
num=123 str=ABC
num = 123 str = ABC
num=123 str=ABC
The following format specifiers can be used with Printf(). %4d prints a four-character integer, and %04d prints a four-character integer padded with zeros.
%v default format
%#v Go syntax representation that shows the type structure
%t bool
%d integer in decimal notation
%s string
%c character
%f fixed-point number
%F fixed-point number
%e floating-point number with e
%E floating-point number with E
%g compact floating-point representation; automatically selects %f or %e
%G compact floating-point representation
%b binary
%o octal
%O octal including the 0o prefix
%x hexadecimal with lowercase letters (a-f)
%X hexadecimal with uppercase letters (A-F)
%U Unicode
%p pointer
%q quoted "..." string
%T type
%% percent sign