GO Program to add two arrays

How to add two arrays

Here we are explaining how to add two arrays in the Go program. We can use the built-in function fmt.println() to print anything and fmt.scanln() for reading the values. This function is defined under the fmt package and it helps to write standard output. In order to use these functions, we need to import the “fmt” package.


How to add two arrays in the GO Program

We are using fmt.println() function for printing the string to the output screen. Here we are showing to add two arrays in the Go language. Here variable size holds the size of the array, and other variables Arr1, Arr2, sumArr holds the array elements. First, read the array elements using for loop. After that add the two arrays as sumArr[i] = Arr1[i] + Arr2[i]. Finally, print the result array sumArr.Given below are the steps which are used in the Go program. 


STEP 1: Import the package fmt

STEP 2: Start function main()

STEP 3: Read the size of the array using fmt.Scanfln()

STEP 4: Declare the arrays variable Arr1, Arr2, sumArr

STEP 5: Read the first array using for loop

STEP 6: Read the second array using for loop

STEP 7: Add the two arrays by adding sumArr[i] = Arr1[i] + Arr2[i]

STEP 8: Print the result array sumArr using fmt.Println()


GO Source Code

                                          package main
import "fmt"

func main() {
    var size, i int
    fmt.Print("Enter the array size = ")

    Arr1 := make([]int, size)
    Arr2 := make([]int, size)
    sumArr := make([]int, size)

    fmt.Println("Enter the first array items = ")
    for i = 0; i < size; i++ {

    fmt.Println("Enter the second array items = ")
    for i = 0; i < size; i++ {

    fmt.Println("The sum of two arrays = ")
    for i = 0; i < size; i++ {
        sumArr[i] = Arr1[i] + Arr2[i]
        fmt.Println(sumArr[i], "  ")


Enter the array size = 5
Enter the first array items = 1 2 3 4 5
Enter the second array items = 6 7 8 9 10
The sum of two arrays = 7  9  11  13  15