Created
March 6, 2013 12:16
-
-
Save smagch/5098919 to your computer and use it in GitHub Desktop.
I'm learning Go
This file contains hidden or 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 ( | |
| "os" | |
| "log" | |
| ) | |
| func open(name string, ch chan []byte, errch chan error) { | |
| file, err := os.Open(name) | |
| if err != nil { | |
| errch <- err | |
| return | |
| } | |
| data := make([]byte, 1000) | |
| count, err := file.Read(data) | |
| if err != nil { | |
| errch <- err | |
| return | |
| } | |
| ch <- data[:count] | |
| } | |
| func main() { | |
| log.Print("start") | |
| ch := make(chan []byte) | |
| errch := make(chan error) | |
| go open("bar.go", ch, errch) | |
| go open("barb.go", ch, errch) | |
| go open("hoge.go", ch, errch) | |
| for i := 0; i < 3; i++ { | |
| select { | |
| case bytes := <-ch: | |
| log.Println(string(bytes)) | |
| if i == 2 { | |
| log.Println("finished") | |
| return | |
| } | |
| case err := <-errch: | |
| log.Fatal(err) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment