In this blog we will look into detail into two print functions in Go – Println()
and Printf()
You may be wondering why a blog on this topic, when there are so many interesting topics to learn in Go. The reason for this is that as you will start learning and writing programs in Go, sooner or later you are going to feel the need to print data to console.
You may want to understand how your program is running, what is it’s current state, what values do the variables hold, etc. The easiest way to find is to print output to console (or to any other output stream). Hence the reason for this writing this blog.
This blog is part of the series – Learn Go, in which we will learn about Go language step by step.
Table of Contents
Note: To use both the Print functions given below, you will need to import the
fmt
package in your code.
1.0 Println()
- Provided by
fmt
package - Print all the arguments to console output.
- If more than one argument, then the arguments are separated by a
space.
- Prints a New Line Character at the end.
- Similar to
System.out.println()
inJava
.
Example 1 : Printing a simple String message
// Will print - Hello World
fmt.Println("Hello World!")
Example 2 : Printing multiple strings
// Will print - Message 1 Message 2 Message 3
fmt.Println("Message 1", "Message 2", "Message 3")
Example 3 : Printing integers
// Will print - 10 15 20 25 35
fmt.Println(10, 15, 20 , 25, 35)
Example 4 : Printing variables of different data types
// Will print - 100 dummy false 11.5
num, str, test, floatNum := 100, "dummy", false, 11.5
fmt.Println(num, str, test, floatNum)
2.0 Printf()
This is also provided by the fmt
package in Go, and is used for printing formatted strings to console.
2.1 Why the need for Printf, when we have Println
There are scenarios, where we need print a formatted string to console.
fmt.Println
does not provides much help in formatting.
e.g Suppose we have following variable declaration,
num, str, test, floatNum := 100, "dummy", false, 11.5
and we want to print the above variables in this format
Number=100, String=dummy, Boolean=false, Floating=11.50
Now if we use fmt.Println()
to print the above message, we may end up writing a statement which looks something like this below.
fmt.Println("Number=", num, "String=", str, "Boolean=", test, "Floating=", floatNum)
But this also does not exactly matches our requirement, as it prints out following
Number= 100 String= dummy Boolean= false Floating= 11.5
Notice the extra spaces that came after the = sign, and the format of floating number is 11.5 instead of 11.50.
fmt.Printf()
comes to our rescue and helps us format the strings.
For. e.g. to print the string in the above required format, we can write a simple Printf()
statement as below
fmt.Printf("Number=%d, String=%s, Boolean=%t, Floating=%0.2f", num, str, test, floatNum)
2.2 Understanding Printf syntax
Let’s look at the above Printf statement again to understand it
fmt.Printf("Number=%d, String=%s, Boolean=%t, Floating=%0.2f", num, str, test, floatNum)
If you look at the above example, it contains following
- A string to that is to be printed to console –
"Number=%d, String=%s, Boolean=%t, Floating=%0.2f"
- String contains many placeholder verbs, like
%d
,%0.2f
, etc which are the formatting syntax for actual arguments. - After the string are a comma separated list of actual arguments (that will be formatted and replaced in the strings) –
num, str, test, floatNum
- For e.g. the last argument is
floatNum
whose value is11.5
, and last placeholder verb is%0.2f
. So 11.5 is formatted to the format matching the specification of%0.2f
, and then replaced in the actual string to be printed. - Same happens for each placeholder and arguments.
- As a result following string gets printed to console
Number=100, String=dummy, Boolean=false, Floating=11.50
2.3 Printf Formatting verbs
Below are the some of the most commonly using formatting verbs in Printf
, along with some examples of how they format a input value to output value
Some example of formatting verbs are
Data Type | Format | Description | Sample Input | Sample Output |
Integer | %d | Prints Integer in Base 10 format | 234 | 234 |
Integer | %+d | Always prints the Sign of the Integer | 234 | +234 |
Integer | %5d | Prints Integer with width of 5, pads with spaces on the left | 234 | 234 (two spaces followed by 234) |
Integer | %-5d | Prints Integer with width of 5, pads with spaces on the right | 234 | 234 (234 followed by two spaces) |
Integer | %05d | Prints Integer with width of 5, pads with Zeros on the left | 234 | 00234 |
Integer | %b | Prints Binary (Base 2) | 234 | 11101010 |
Integer | %o | Prints Octal (Base 8) | 234 | 352 |
Boolean | %t | Prints true or false | true | true |
Floating Point | %f | Decimal point, pads 0 to end of decimal, if less than 6 digits, else truncates it | 123.465 | 123.465000 |
Floating Point | %f | -Same as above- | 123.1010101010 | 123.101010 |
Floating Point | %g | Necessary digits only. | 123.465 | 123.465 |
Floating Point | %g | -Same as above- | 123.1010101010 | 123.101010101 |
Floating Point | %e | Prints in Exponential format | 123.465 | 1.234650e+02 |
Floating Point | %0.2f | Prints only 2 decimal digits (after rounding up) | 123.465 | 123.47 |
String | %s | Prints plain String | Sunit | Sunit |
String | %10s | Prints atleast 10 characters, adding space to left (if required) | Sunit | Sunit (adds 5 spaces to the start of string) |
String | %-10s | Prints atleast 10 characters, adding space to right (if required) | Sunit | Sunit (adds 5 spaces to the end of string) |
String | %q | Prints quoted string | Sunit | “Sunit” |
Integer | %T | Prints the data type | 234 | int |
Floating Point | %T | -Same as above- | 123.1010101010 | float64 |
String | %T | -Same as above- | Sunit | string |
Boolean | %T | -Same as above- | true | bool |
Note:
%v
, can be used for most common scenarios.
The default format for%v
is:
Boolean: %t
Integers: %d
Floating Point: %g
String: %s
For more details, please refer to Go fmt documentation
In the next blog of the Learn Go tutorial series, we will look at Writing your first Go Program