Hi,
I have an array of array string ([][]string{}). I am looping through them
and trying to sum the second value of the arrays based on the first value.
For example:
a := []string{"a", "1"}
a1 := []string{"a", "2"}
a2 := []string{"a", "3"}
b := []string{"b", "4"}
b1 := []string{"b", "1"}
Sum all the 'a' values and 'b' values.
Here's the sample code:
package main
import (
"fmt"
"log"
"strconv"
)
func main() {
a := []string{"a", "1"}
a1 := []string{"a", "2"}
a2 := []string{"a", "3"}
b := []string{"b", "4"}
b1 := []string{"b", "2"}
b2 := []string{"b", "1"}
dataarray := [][]string{}
dataarray = append(dataarray, a, a1, a2, b, b1, b2)
var totalSum float64
for j, _ := range dataarray {
sumFloat, err := strconv.ParseFloat(dataarray[j][1], 64)
if err != nil {
log.Fatal(err)
}
totalSum += sumFloat
}
fmt.Println(totalSum)
}
Right now, its summing all the values. The result I want is:
a: 6
b: 7
Let me know if there's a way to do this. Here's the link to go
playground: https://play.golang.org/p/KL8GQ7LPJNi
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.