Created
April 14, 2016 19:44
-
-
Save 9nut/b874afee6a3cae629c9adc689d195884 to your computer and use it in GitHub Desktop.
This file contains 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" | |
"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