Skip to content

Instantly share code, notes, and snippets.

@donovanhide
Last active December 10, 2015 01:58
Show Gist options
  • Save donovanhide/4363771 to your computer and use it in GitHub Desktop.
Save donovanhide/4363771 to your computer and use it in GitHub Desktop.
$ go version && go run test.go
go version devel +87f67aadaed6 Sat Dec 22 17:41:00 2012 -0800 darwin/amd64
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test1: 0
Test2: 0
Test2: 1
Test2: 2
Test2: 3
Test2: 4
Test2: 5
Test2: 6
Test2: 7
Test2: 8
Test2: 9
$ go version && go run test.go
go version go1.0.3
Test1: 0
Test1: 1
Test1: 2
Test1: 3
Test1: 4
Test1: 5
Test1: 6
Test1: 7
Test1: 8
Test1: 9
Test2: 0
Test2: 1
Test2: 2
Test2: 3
Test2: 4
Test2: 5
Test2: 6
Test2: 7
Test2: 8
Test2: 9
package main
import (
"fmt"
"time"
)
type Map map[time.Time]int
type Slice []time.Time
func test1(m Map, s Slice, c chan int) {
ticker := time.NewTicker(time.Millisecond)
for _, v := range s {
c <- m[v]
<-ticker.C
}
close(c)
}
func test2(m Map, s Slice, c chan int) {
for _, v := range s {
c <- m[v]
time.Sleep(time.Millisecond)
}
close(c)
}
func main() {
m := make(Map)
s := make(Slice, 0)
for i := 0; i < 10; i++ {
now := time.Now()
m[now] = i
s = append(s, now)
}
c1 := make(chan int)
go test1(m, s, c1)
for i := range c1 {
fmt.Println("Test1:", i)
}
c2 := make(chan int)
go test2(m, s, c2)
for i := range c2 {
fmt.Println("Test2:", i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment