Skip to content

Instantly share code, notes, and snippets.

@thbourlove
Created July 28, 2016 13:51
Show Gist options
  • Save thbourlove/e7dc45afbe3aea5bc9f7dc810cdd4d63 to your computer and use it in GitHub Desktop.
Save thbourlove/e7dc45afbe3aea5bc9f7dc810cdd4d63 to your computer and use it in GitHub Desktop.
read stdin with timeout
package main
import (
"fmt"
"log"
"syscall"
)
func main() {
rfdset := syscall.FdSet{Bits: [16]int64{int64(1)}}
timeout := syscall.Timeval{Sec: 1}
n := -1
var err error
n, err = syscall.Select(1, &rfdset, nil, nil, &timeout)
if err != nil {
log.Println("select : ", err)
}
if n == 1 {
var number int
_, err = fmt.Scanf("%d", &number)
if err != nil {
log.Println("scanf : ", err)
}
log.Println("number: ", number)
} else {
log.Println("timeout.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment