fmt package supports two closely-related functions for formatting a string to be displayed on the terminal.
.Print() accepts strings as arguments and concatenates them without any spacing.
.Println(), on the other hand, adds a space between strings and appends a new line to the concatenated output string.
fmt.Print("I", "am", "cool")// Iamcoolfmt.Println("I", "am", "cool")// I am cool
.Printf() function in
fmt provides custom formatting of a string using one or more verbs. A verb is a placeholder for a named value (constant or variable) to be formatted according to these conventions:
%vrepresents the named value in its default format
%dexpects the named value to be an integer type
%fexpects the named value to be a float type
%Trepresents the type for the named value
The first argument for
.Printf() is the string with verb(s) followed by one or more named values corresponding to the verb(s). Unlike
.Printf() does not append a newline to the formatted string.
name := "Leslie"fmt.Printf("My name is %v", name)// My name is Leslieage := 34fmt.Printf("I am %d years old", age)// I am 34 years oldfmt.Printf("%v is of type %T", name, name)// Leslie is of type string
In Go, fmt‘s Scan() method allows users to input information. The function accepts an argument of an address to be scanned into.
var number intfmt.Println("What is your favorite number?")fmt.Scan(&number)fmt.Printf("Your favorite number is %d.\n", number)