Created
December 26, 2015 14:20
-
-
Save dpapathanasiou/0b013d2e5262d56a9f75 to your computer and use it in GitHub Desktop.
Concurrency programming example: test user reflexes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you have an error in line 32; should be "entered.Sub(paused)"