Go Program To Add Two Numbers

Hello coders, in this post we will learn how to add two numbers using Go Programming language.

Hello coders, in this post we will learn how to add two numbers using Go Programming language.

We will be using different methods to add two numbers in Go language. 

So, without any delay, let's begin the tutorial.

Go Program To Add Two Numbers
Table Of Contents 👊

Go Program To Add Two Numbers

package main
import "fmt"

func main(){
  var sum int
  a := 5
  b := 7
  sum = a + b
  fmt.Println("The Sum of a and b is ", sum)
}

Output

The Sum of a and b is 12

How Does This Program Work ?

  var sum int

In this program, we have declared an int data type variable named as sum.

  a := 5
  b := 7

Then, we have declared two constant a and b whose value is 5 and 7 respectively. The := syntax is shorthand for declaring and initializing a variable.

  sum = a + b

Sum is calculated using + operator.

  fmt.Println("The Sum of a and b is ", sum)

Finally, the sum of these two numbers is printed using fmt.Println() function.

Some of the used terms in this program are as follows:- 

package main - packages are a collection of Go source files that reside in the same directory. The line package main specifies that this file belongs to the main package. 

Every Executable Go Application must contain the main function.

fmt - fmt stands for Format package. This package is all about formatting input and output.

:= - The := syntax is shorthand for declaring and initializing a variable.

fmt.Println() - The fmt.Println() function in Go language is used to display output.

Go Program To Add Two Numbers Using User Input

package main 
import "fmt"

func main(){
  var a, b, sum int
  
  // Asking for Input
  fmt.Println("Enter First Number: ")
  fmt.Scanln(&a)
  
  fmt.Println("Enter Second Number: ")
  fmt.Scanln(&b)
  
  sum = a + b
  fmt.Println("The Sum of a and b is", sum)
}

Output

Enter First Number: 
8
Enter Second Number: 
7
The Sum of a and b is 15

fmt.Scanln() function is used to take input from the user in Go Programming language.

Go Program To Add Two Numbers Using Functions

// Go Program To Add Two Numbers
package main
import "fmt"

func add(x, y int) int {
  return x + y
}

func main(){
  var a, b int
  
  // Asking for Input 
  fmt.Println("Enter First Number: ")
  fmt.Scanln(&a)
  
  fmt.Println("Enter Second number: ")
  fmt.Scanln(&b)
  
  fmt.Println("The sum of a and b is", add(a,b))
}

Output

Enter First Number: 
7
Enter Second number: 
18
The sum of a and b is 25

func add(x, y int) int {
  return x + y
}

Here, in this program we have declared a user defined function which returns the sum of two numbers as output.

Conclusion

I hope after going reading this post, you clearly understood how to add two numbers using Go Programming language. 

If you still have any doubt regarding the topic, let us know in the Comment Section.

Sloth Coders is a Learning Platform for Coders, Programmers and Developers to learn from the basics to advance of Coding of different langauges(python, Java, Javascript and many more).

Post a Comment