Skip to content

Instantly share code, notes, and snippets.

@mneko22
Created April 4, 2020 15:43
Show Gist options
  • Select an option

  • Save mneko22/c7cecde5401e557400bdbe7d7ae359da to your computer and use it in GitHub Desktop.

Select an option

Save mneko22/c7cecde5401e557400bdbe7d7ae359da to your computer and use it in GitHub Desktop.
typing game sample
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