You can use a map to keep track of a letters sum. Here's your example 
slightly modified (it uses float64 as you were using 
that): https://play.golang.org/p/98L9fDXSN_A

El lunes, 5 de marzo de 2018, 15:28:54 (UTC-3), Ashish Timilsina escribió:
>
> 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.

Reply via email to