Last active
April 25, 2020 05:21
-
-
Save foolishway/861312b62bb9c0191ca5852ac6c4c4cb 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" | |
"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