DEV Community

Cover image for How to Print in GoLang
Steve Yonkeu
Steve Yonkeu

Posted on

How to Print in GoLang

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!")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

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!")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Alice, Age: 25
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Alice, Age: 25
Enter fullscreen mode Exit fullscreen mode

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!")
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

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 ❌ ✅ ✅ ❌

Take a look at my YouTube and GitHub

Top comments (0)