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.

Updated on: 26-Aug-2022

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements