Created
April 20, 2011 23:25
-
-
Save beatgammit/933321 to your computer and use it in GitHub Desktop.
Issue using channels with os.File.Read in 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 ( | |
"web" | |
"os" | |
"fmt" | |
"container/vector" | |
"time" | |
) | |
var listeners vector.Vector | |
func listen (ctx *web.Context) { | |
c := make(chan []byte) | |
listeners.Push(c) | |
fileData := <-c | |
fmt.Println("Push to client") | |
for fileData != nil { | |
ctx.Write(fileData) | |
fileData = <-c | |
} | |
} | |
func serve (path string) { | |
file, err := os.Open(path, os.O_RDONLY, 0666) | |
if err != nil { | |
panic(err.String()) | |
} | |
defer listeners.Resize(0, 0) | |
defer file.Close() | |
buf := make([]byte, 512) | |
_, err = file.Read(buf) | |
fmt.Println("Serve data") | |
for err != os.EOF { | |
for _, val := range listeners { | |
c := val.(chan []byte) | |
c <- buf | |
} | |
fmt.Println("Read next") | |
_, err = file.Read(buf) | |
} | |
fmt.Println("End of read") | |
for _, val := range listeners { | |
c := val.(chan []byte) | |
c <- nil | |
} | |
} | |
func socketInit (path string) { | |
for { | |
time.Sleep(5 * 1000 * 1000000) // sleep for 5 seconds | |
serve(path) | |
} | |
} | |
func main() { | |
go socketInit("testServer.go") | |
web.Get(".*", listen) | |
web.Run("localhost:13240") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment