Skip to content

Instantly share code, notes, and snippets.

@chespinoza
Created September 2, 2013 04:57
Show Gist options
  • Save chespinoza/6409314 to your computer and use it in GitHub Desktop.
Save chespinoza/6409314 to your computer and use it in GitHub Desktop.
Playing with Go data structures...
package main
import (
"fmt"
"math/rand"
"time"
)
type Cc struct {
name string
dep int
date time.Time
}
type Aux struct {
accum int
cant int
}
func main() {
var ccs []Cc
horas := make(map[int]Aux)
f1 := time.Now()
for i := 0; i < 10; i++ {
f1 = f1.Add(20 * time.Minute)
ccs = append(ccs, Cc{name: "cespinoza", dep: rand.Intn(1000), date: f1})
}
for _, e := range ccs {
for i := 0; i < 25; i++ {
if e.date.Hour() == i {
horas[i] = Aux{horas[i].accum + e.dep, horas[i].cant + 1 }
}
}
fmt.Printf("%v %v %v\n", e.name, e.dep, e.date)
}
fmt.Println(horas)
}
@chespinoza
Copy link
Author

Finally I'm getting ten records , maps with the value's sum for every hour and cantity, ready to average by hour:

hour:{sum values_cant}, ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment