Skip to content

Instantly share code, notes, and snippets.

@nf
Created July 12, 2010 11:34
Show Gist options
  • Save nf/472384 to your computer and use it in GitHub Desktop.
Save nf/472384 to your computer and use it in GitHub Desktop.
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