How to calculate the average length of the strings in string array with Go

1 Answer

0 votes
package main

import (
	"fmt"
)

func main() {
	arr := []string{"go", "c", "c++", "java", "python"}
	totalLength := 0

	for _, str := range arr {
		totalLength += len(str)
	}

	average := float64(totalLength) / float64(len(arr))

	fmt.Println(average)
}



/*
run:

3.2

*/

 



answered Aug 19, 2024 by avibootz
...