It has always been a challenge for me when working with GoLang and trying to print out something, usually during debugging and or various other tasks while building.
Let's have a quick look at what we should use and when should we use it with regards to printing in GoLang.
PS: Try to read through, we will make it fast as much as possible.
Personally I usually use about 7 of the print methods in-built in GoLang, which include: fmt.Print
, fmt.Println
, fmt.Printf
, fmt.Sprint
, fmt.Sprintln
, fmt.Sprintf
and fmt.Fprint
. In Go (golang
), the fmt
package provides several printing functions for different use cases.
1. fmt.Print
- Purpose: Prints values directly to the console.
- No Formatting Specifiers: Just concatenates values as strings.
- No Line Break: No newline at the end, so subsequent output appears on the same line.
Example:
fmt.Print("Hello", " ", "World!")
Output:
Hello World!
2. fmt.Println
- Purpose: Prints values directly to the console, with a newline at the end.
- No Formatting Specifiers: Concatenates values with spaces between them.
- Auto Line Break: Automatically adds a newline at the end.
Example:
fmt.Println("Hello", "World!")
Output:
Hello World!
3. fmt.Printf
- Purpose: Prints values directly to the console with formatting specifiers.
-
Formatting Specifiers: Uses
%s
,%d
,%v
, etc. -
No Auto Line Break: Doesn't automatically add a newline; you need to specify
\n
.
Example:
fmt.Printf("Name: %s, Age: %d\n", "Alice", 25)
Output:
Name: Alice, Age: 25
4. fmt.Sprint
- Purpose: Concatenates values into a string without formatting.
- No Printing: Doesn't print to the console, just returns the string.
- No Line Break: No newline included.
Example:
message := fmt.Sprint("Hello", " ", "World!")
fmt.Println(message)
Output:
Hello World!
5. fmt.Sprintln
- Purpose: Concatenates values into a string with a newline.
- No Printing: Doesn't print to the console, just returns the string.
- Auto Line Break: Adds a newline to the returned string.
Example:
message := fmt.Sprintln("Hello", "World!")
fmt.Print(message)
Output:
Hello World!
6. fmt.Sprintf
- Purpose: Formats values into a string using format specifiers.
- No Printing: Doesn't print to the console, just returns the formatted string.
- No Line Break: No newline unless explicitly added.
Example:
message := fmt.Sprintf("Name: %s, Age: %d", "Alice", 25)
fmt.Println(message)
Output:
Name: Alice, Age: 25
7. fmt.Fprint
- Purpose: Writes formatted output to a specified writer (like files, buffers, etc.).
- For Advanced Usage: Typically used with files or custom outputs.
Example:
import (
"os"
"fmt"
)
func main() {
fmt.Fprint(os.Stdout, "Hello World!")
}
Output:
Hello World!
Summary Table:
Function | Purpose | Returns a String? | Directly Prints? | Supports Formatting? | Auto Newline? |
---|---|---|---|---|---|
fmt.Print |
Simple printing, no newline | ❌ | ✅ | ❌ | ❌ |
fmt.Println |
Simple printing with newline | ❌ | ✅ | ❌ | ✅ |
fmt.Printf |
Formatted printing | ❌ | ✅ | ✅ | ❌ |
fmt.Sprint |
String concatenation (no print) | ✅ | ❌ | ❌ | ❌ |
fmt.Sprintln |
String concatenation with newline | ✅ | ❌ | ❌ | ✅ |
fmt.Sprintf |
Formatted string creation | ✅ | ❌ | ✅ | ❌ |
fmt.Fprint |
Formatted printing to a writer | ❌ | ✅ | ✅ | ❌ |
Top comments (0)