Skip to content
Home » How to read and print an integer variable in Golang

How to read and print an integer variable in Golang

Learn about How to read and print an integer variable in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to read and print an integer variable in Golang

//Golang program to read and print an integer variable.

package main
import "fmt"

func main() {
    //Declare an integer type variable
    var num int = 0
    
    fmt.Print("Enter the value of num: ") 
    fmt.Scanf("%d",&num)
    
    fmt.Println("Value of num is: ",num) 
}

In the above program, we declare the package main. The main package instructs the Go language compiler that the package must be compiled and an executable file generated. We imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. Now, we come to the main() function. The main() function is the entry point for the program. Here, we created a variables num. Then we read the value of the num variable using the scanf() function and then print the value of the num variable on the console screen.

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Go program to find the average of three numbers
Go program to add two numbers