Skip to content

Instantly share code, notes, and snippets.

@foolishway
Last active April 25, 2020 05:21
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 foolishway/861312b62bb9c0191ca5852ac6c4c4cb to your computer and use it in GitHub Desktop.
Save foolishway/861312b62bb9c0191ca5852ac6c4c4cb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"log"
"os"
)
func main() {
f, err := os.Open("./cat.txt")
defer f.Close()
if err != nil {
log.Fatal("Open file error: ", err)
}
bchan := make(chan []byte)
go func() {
defer close(bchan)
for {
b := make([]byte, 1)
if _, err := f.Read(b); err == nil {
bchan <- b
} else {
break
}
}
}()
loop:
for {
select {
case b, ok := <-bchan:
if ok {
time.Sleep(100 * time.Millisecond)
fmt.Printf("%s",b)
} else {
fmt.Println()
break loop;
}
}
}
fmt.Println("main is eixt.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment