Created
April 4, 2020 15:43
-
-
Save mneko22/c7cecde5401e557400bdbe7d7ae359da to your computer and use it in GitHub Desktop.
typing game sample
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 ( | |
| "fmt" | |
| "context" | |
| "time" | |
| "bufio" | |
| "os" | |
| ) | |
| func main() { | |
| bc := context.Background() | |
| ctx, _ := context.WithTimeout(bc, 5 * time.Second) | |
| input := make(chan string) | |
| strs := []string{"c", "python", "fortran", "cobol", "brain f**k"} | |
| score := 0 | |
| go func () { | |
| scanner := bufio.NewScanner(os.Stdin) | |
| for scanner.Scan() { | |
| input <- scanner.Text() | |
| } | |
| }() | |
| go func () { | |
| var str string | |
| for i := 0; i < len(strs); i++ { | |
| fmt.Println("q: ", strs[i]) | |
| select { | |
| case str = <- input: | |
| if str == strs[i] { | |
| score++; | |
| } | |
| } | |
| } | |
| }() | |
| select { | |
| case <-ctx.Done(): | |
| fmt.Println("score: ", score) | |
| return | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment