Skip to content

Instantly share code, notes, and snippets.

@maiah
Last active October 20, 2015 23:50
Show Gist options
  • Save maiah/a1bc9d0b9249346f4f99 to your computer and use it in GitHub Desktop.
Save maiah/a1bc9d0b9249346f4f99 to your computer and use it in GitHub Desktop.
Reader wrapper to have functional programming style of reading io (no mutables)
package main
import (
"fmt"
"io"
"os"
)
func main() {
f, err := os.Open("./temp")
defer f.Close()
check(err)
res := Read(f, 5)
fmt.Printf(string(res))
res = Read(f, 5)
fmt.Printf(string(res))
res = Read(f, 5)
fmt.Printf("%s\n", string(res))
}
// Reader wrapper to avoid mutable slice of byte
func Read(r io.Reader, size int) []byte {
d := make([]byte, size)
r.Read(d)
return d
}
func check(e error) {
if e != nil {
panic(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment