-
-
Save azr/db05b6eb596604205d8b to your computer and use it in GitHub Desktop.
Concurrency programming example: test user reflexes
This file contains hidden or 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() { | |
goChan := make(chan time.Time) | |
go func() { | |
time.Sleep(time.Second * time.Duration(rand.Intn(3))) | |
fmt.Println("Go !") | |
goChan <- time.Now() | |
}() | |
bufio.NewReader(os.Stdin).ReadString('\n') | |
userTime := time.Now() | |
select { | |
case goTime := <-goChan: | |
fmt.Printf("%s\n", userTime.Sub(goTime)) | |
default: | |
fmt.Println("Failed !") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment