Skip to content

Instantly share code, notes, and snippets.

@9nut
Created April 14, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9nut/b874afee6a3cae629c9adc689d195884 to your computer and use it in GitHub Desktop.
Save 9nut/b874afee6a3cae629c9adc689d195884 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io"
"os"
"time"
)
func main() {
tcopy(os.Stdout, os.Stdin, 1*time.Second)
}
func tcopy(wr io.Writer, rd io.Reader, tout time.Duration) {
rdsig := make(chan int)
buf := make([]byte, 1024)
F:
for {
go func() {
n, err := rd.Read(buf)
if err != nil {
fmt.Println("read error", err)
close(rdsig)
} else {
rdsig <- n
}
}()
select {
case <-time.After(tout):
fmt.Println("read timeout, exiting")
break F
case _, ok := <-rdsig:
if !ok {
break F
}
}
wr.Write(buf)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment