Go fmt Package
The fmt package (short for "format") is the most commonly used package for printing and reading data.
1. Printing Functions
There are three main ways to print output:
fmt.Println: Prints arguments with spaces between them and a newline at the end.fmt.Print: LikePrintln, but no newline or extra spaces.fmt.Printf: Used for formatted strings (incorporates "verbs" like%d).
2. Creating Strings (Sprintf)
If you want to "print" to a variable instead of the console, use fmt.Sprintf.
3. Reading Input (Scan)
To get input from the user, use fmt.Scan or fmt.Scanln.
4. Errors (Errorf)
fmt.Errorf is a quick way to create a formatted error message.
Quick Summary
| Function | Description |
|---|---|
Print / Println |
Basic console output. |
Printf |
Formatted console output. |
Sprintf |
Returns a formatted string. |
Scan |
Reads input from console. |
Errorf |
Creates a formatted error. |