Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Print an Integer in Golang?
In this tutorial, we will learn how to print an integer in the Golang Programming language. The fmt package is used to perform the input/output operations which are comparable to the input/output functions in C i.e scanf and printf. Also, format specifiers are referred from C but in Golang they are simple. We will discuss what specifiers we have for integers.
Specifiers are something which will tell us the what data type we are printing.
%b ? for base 2
%o ? for bae 8
%d ? for base 10
Functions in fmt package:?
-
fmt.Printf() ? It prints the output depending on the specifier. Also, it returns the number of bytes it is printing and the error.
-
fmt.Println() ? In this function, you can pass the variables and string directly without mentioning the format specifier you have to just separate them by ?,?. The spaces will be added automatically between them and a new line will get append at the end.
Different ways to print an Integer in Golang
In Golang there are two ways to define integers. We will explore both ways one by one.
Var keyword
First by using the var keyword.
Syntax
Integer declaration using the var keyword Var integerName int
Algorithm
STEP 1 ? START
STEP 2 ? Declare the Integer using the var keyword
STEP 3 ? initialize the variable
STEP 4 ? Printing it on the console
STEP 5 ? STOP
Example 1
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything </span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// defining integer with var keyword</span>
<span class="token keyword">var</span> currentYear <span class="token keyword">int</span>
<span class="token comment">// initialize the variable</span>
currentYear <span class="token operator">=</span> <span class="token number">1976</span>
<span class="token comment">// printing the variable using println() function</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"What is the current year? It's"</span><span class="token punctuation">,</span> currentYear<span class="token punctuation">,</span> <span class="token string">"Using Println function"</span><span class="token punctuation">)</span>
<span class="token comment">// printing the variable using printf() function</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"What is the current year? It's %d using Printf function "</span><span class="token punctuation">,</span> currentYear<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
What is the current year? It's 1976 Using Println function What is the current year? It's 1976 using Printf function
In above example firs we are defining an integer variable name currentYear using var keyword then we are initializing it. After that using fmt package function Println() we are printing the integer variable. In last we are printing the integer variable with other function name Printf() in fmt package.
Short hand way
Now we will explore the other way to declare the variable and print it.
Syntax
Integer declaration using shorthand method integerName:= initialize with value
STEP 1 ?START
STEP 2 ? Declare the Integer and its value by shorthand method
STEP 3 ? Printing it on the console
STEP 4 ? STOP
Example 2
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything </span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// define and initialize the variable</span>
currentYear <span class="token operator">:</span><span class="token operator">=</span> <span class="token number">1976</span>
<span class="token comment">// printing the variable using println() function</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Println</span><span class="token punctuation">(</span><span class="token string">"What is the current year? It's"</span><span class="token punctuation">,</span> currentYear<span class="token punctuation">,</span> <span class="token string">"Using Printlnfunction"</span><span class="token punctuation">)</span>
<span class="token comment">// printing the variable using printf() function</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"What is the current year? It's %d using Printf function "</span><span class="token punctuation">,</span> currentYear<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
What is the current year? It's 1976 Using Println function What is the current year? It's 1976 using Printf function
In above example firs we are defining an integer variable name currentYear using shorthand way then we are initializing it. After that using fmt package function Println() we are printing the integer variable. In last we are printing the integer variable with other function name Printf() in fmt package.
Conclusion
If you are using the Println() function the benefit of that is that you don?t have to take care of format specifier which is not in case of Printf() function
The shorthand technique benefit is that it will automatically type cast the variable without mentioning the data type.
This is all about printing the integer, the packages, and functions we can use to print the integer, and also different ways to declare an integer. To learn more about go you can visit this site.
