In this Go program, we declare two integer values and then add those two numbers.
package main import "fmt" func main() { var sum int num1 := 10 num2 := 25 sum = num1 + num2 fmt.Println("The Sum of num1 and num2 = ", sum) }
The Sum of num1 and num2 = 35
This golang program allows user to enter two integers and then add those numbers.
package main import "fmt" func main() { var num1, num2 int fmt.Print("Enter the First Number = ") fmt.Scanln(&num1) fmt.Print("Enter the Second Number = ") fmt.Scanln(&num2) fmt.Println("The Sum of num1 and num2 = ", num1 + num2) }
Enter the First Number = 100
Enter the Second Number = 137
The Sum of num1 and num2 = 237
Go Program to Add Two Numbers using Functions
In this golang program, we created a new function that accepts two integers and returns the addition. Within the program main function, we call that function by passing user-entered values.
package main import "fmt" func add(x, y int) int { return x + y } func main() { var num1, num2 int fmt.Print("Enter the First Number = ") fmt.Scanln(&num1) fmt.Print("Enter the Second Number = ") fmt.Scanln(&num2) fmt.Println("The Sum of num1 and num2 = ", add(num1, num2)) }