Compare Println vs Printf in Golang with Examples


In Golang, printing messages to the console is a common task for debugging and logging purposes. There are two main ways to print messages to the console: using Println and Printf. In this article, we will compare these two methods and explore when it is appropriate to use each one.

Println

Println is a built-in function in Golang's fmt package that is used to print messages to the console with a new line at the end. It takes zero or more arguments of any type and separates them with a space. Here is an example −

Example

package main

import "fmt"

func main() {
   fmt.Println("Hello, World!")
   fmt.Println("The answer is:", 42)
   fmt.Println("3 + 4 =", 3+4)
}

Output

Hello, World!
The answer is: 42
3 + 4 = 7

In the example above, we use Println to print three messages to the console. Note that Println automatically adds a new line at the end of each message.

Printf

Printf is another built-in function in Golang's fmt package that is used to print messages to the console with a specified format. It takes a string as its first argument, which can contain one or more format verbs, and one or more values as its subsequent arguments. Here is an example −

Example

package main

import "fmt"

func main() {
   fmt.Printf("The answer is: %d\n", 42)
   fmt.Printf("Pi is approximately: %f\n", 3.14159)
   fmt.Printf("My name is %s and I am %d years old.\n", "John", 30)
}

Output

The answer is: 42
Pi is approximately: 3.141590
My name is John and I am 30 years old.

In the example above, we use Printf to print three messages to the console. Note that we use format verbs in the string argument to specify the format of the subsequent values. For example, %d is a format verb for an integer, and %f is a format verb for a floating-point number.

Comparison

Both Println and Printf can be used to print messages to the console in Golang, but there are some differences between them that should be considered. Here are some points to keep in mind −

  • Println automatically adds a new line at the end of each message, while Printf does not. This means that if you use Printf to print multiple messages on the same line, you need to manually add a new line character at the end of the last message.

  • Printf allows you to specify the format of the printed values using format verbs, which can be useful for printing values in a specific format. However, if you don't need to specify a format, Println can be a simpler and more concise way to print messages.

  • Printf can be more efficient than Println when printing large amounts of data, as it avoids the need to concatenate strings with the + operator.

Conclusion

In conclusion, Println and Printf are both useful functions for printing messages to the console in Golang. Println is a simple and concise way to print messages with a new line at the end, while Printf allows you to specify the format of the printed values. When choosing between these two methods, consider the formatting needs of your messages and the efficiency of your code.

Updated on: 07-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements