Given an array of n elements, your task is to find out the *average of the array* in Golang.

You need to traverse array and then sum all of the values.

Finally, you need to divide sum to length of the array. You should make this variable as float because if you do it with integers then your result also will be integer which might not be correct. In other words, after calculating the sum, we **must convert the data types** of the *sum* and *size of the array* to **float**, so that we don’t lose any decimal values.

package main import "fmt" func main() { // declaring an array of values arra := []int{1, 2, 3, 4} // declaring a variable // to store the sum sum := 0 // traversing through the // array using for loop for i := 0; i < len(arra); i++ { // adding the values of // array to the variable sum sum += arra[i] } // declaring a variable // avg to find the average avg := (float64(sum)) / (float64(len(arra))) // typecast all values to float // to get the correct result fmt.Println("Sum = ", sum, "\nAverage = ", avg) }