DEV Community

tanishparashar
tanishparashar

Posted on

3 1 1 1 1

Post2: Golang Print Functions

series: Golang

Printing Functions in Golang

In Golang, several functions are available for printing text, and each serves specific use cases. Here's an explanation of the most commonly used printing functions:

1. fmt.Print

Description:
Prints the provided arguments as plain text without adding a newline. It does not format the output.

Use Case:
For simple concatenated text or values where no specific formatting is required.

fmt.Print("Hello")          // Output: Hello
fmt.Print("World")          // Output: HelloWorld
fmt.Print(123, " GoLang")   // Output: HelloWorld123 GoLang
Enter fullscreen mode Exit fullscreen mode

2. fmt.Println

Description:
Prints the provided arguments as plain text and appends a newline at the end.

Use Case:
For simple outputs where you want automatic line breaks after printing.

fmt.Println("Hello")         // Output: Hello (with newline)
fmt.Println("World")         // Output: World (on a new line)
fmt.Println(123, "GoLang")   // Output: 123 GoLang (on a new line)
Enter fullscreen mode Exit fullscreen mode

3. fmt.Printf

Description:
Formats and prints text according to a specified format string. Does not add a newline unless explicitly included in the format string.

Use Case:
For dynamic or formatted outputs (e.g., integers, floats, strings, etc.).

name := "Alice"
age := 25
fmt.Printf("My name is %s and I am %d years old.", name, age)
// Output: My name is Alice and I am 25 years old.
Enter fullscreen mode Exit fullscreen mode

Common Formatting Verbs:

Verb Description Example
%s String fmt.Printf("%s", "Go")
%d Integer (base 10) fmt.Printf("%d", 123)
%f Floating-point fmt.Printf("%.2f", 3.14)
%v Default format for any value fmt.Printf("%v", true)
%T Type of the variable fmt.Printf("%T", name)
%+v Struct with field names fmt.Printf("%+v", obj)

4. fmt.Sprintf

Description:
Formats text like fmt.Printf, but instead of printing to the console, it returns the formatted string.

Use Case:
For preparing strings for later use (e.g., logging, building responses).

formatted := fmt.Sprintf("Hello, %s!", "Alice")
fmt.Println(formatted)
// Output: Hello, Alice!
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay