Last active
October 20, 2015 23:50
-
-
Save maiah/a1bc9d0b9249346f4f99 to your computer and use it in GitHub Desktop.
Reader wrapper to have functional programming style of reading io (no mutables)
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" | |
"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