Created
July 12, 2010 11:34
-
-
Save nf/472384 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 ( | |
"bufio" | |
"encoding/binary" | |
"flag" | |
"io" | |
"os" | |
) | |
type SomeStruct struct { | |
a, b, c int64 | |
d, e, f int | |
g, h, i byte | |
} | |
func main() { | |
buffer := flag.Bool("buffer", true, "buffer output stream") | |
flag.Parse() | |
v := SomeStruct{ 20, 40, 60, 10, 20, 30, 50, 30, 10 } | |
f, err := os.Open("bin.dat", os.O_WRONLY | os.O_TRUNC | os.O_CREATE, 0666) | |
if err != nil { | |
panic(err) | |
} | |
defer f.Close() | |
var w io.Writer | |
if *buffer { | |
w = bufio.NewWriter(f) | |
} else { | |
w = f | |
} | |
for i := 0; i < 1e6 ; i++ { | |
binary.Write(w, binary.BigEndian, v) | |
} | |
if *buffer { | |
w.(*bufio.Writer).Flush() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment