Home » Go program to add two numbers

Go program to add two numbers

Learn about Go program to add two numbers in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.


Go program to add two numbers

//Golang program to add two integer numbers.

package main
import "fmt"

func main() {
    var num1 int =5
    var num2 int =10
    var num3 int =0
    //Add num1,num2 and assign result to num3
    fmt.Println("Addition is: ",num3)

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 declared three variables num1, num2, num3 that are initialized with 5, 10, 0 respectively.