- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
package main // fmt package provides the function to print anything import "fmt" func main() { // defining integer with var keyword var currentYear int // initialize the variable currentYear = 1976 // printing the variable using println() function fmt.Println("What is the current year? It's", currentYear, "Using Println function") // printing the variable using printf() function fmt.Printf("What is the current year? It's %d using Printf function ", currentYear) }
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
package main // fmt package provides the function to print anything import "fmt" func main() { // define and initialize the variable currentYear := 1976 // printing the variable using println() function fmt.Println("What is the current year? It's", currentYear, "Using Printlnfunction") // printing the variable using printf() function fmt.Printf("What is the current year? It's %d using Printf function ", currentYear) }
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.
- Related Articles
- How to Print an Integer in Swift Program?
- How to Read and Print an Integer value in C++
- Java Program to Print an Integer
- Kotlin Program to Print an Integer
- How to fetch an Integer variable as String in GoLang?
- Golang program to print an array?
- How to print integer array in android Log?
- Golang Program to Print an Identity Matrix
- Golang Program to count the set bits in an integer.
- How to print a string in Golang?
- Different Ways to Convert an Integer Variable to String in Golang
- Golang Program to convert an integer into binary representation
- Golang Program to Print an Inverted Star Pattern
- How to print the ASCII values in Golang?
- How to print struct variables data in Golang?
