Skip to content

Instantly share code, notes, and snippets.

@dpapathanasiou
Created December 26, 2015 14:20
Show Gist options
  • Save dpapathanasiou/0b013d2e5262d56a9f75 to your computer and use it in GitHub Desktop.
Save dpapathanasiou/0b013d2e5262d56a9f75 to your computer and use it in GitHub Desktop.
Concurrency programming example: test user reflexes
package main
import (
"bufio"
"fmt"
"math/rand"
"os"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
channel := make(chan time.Time)
go func() {
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
channel <- time.Now()
}()
time.Sleep(time.Second * time.Duration(rand.Intn(11)))
paused := time.Now()
fmt.Println("GO!")
entered := <- channel
if paused.Sub(entered) > 0 {
fmt.Println("FAIL")
} else {
fmt.Printf("%v\n", time.Since(entered))
}
}
@sytabaresa
Copy link

you have an error in line 32; should be "entered.Sub(paused)"

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