Skip to content

Instantly share code, notes, and snippets.

@phuysmans
Forked from tonetheman/writefile.go
Created October 17, 2017 07:49
Show Gist options
  • Save phuysmans/37b01318823904847a01d019cb154b1a to your computer and use it in GitHub Desktop.
Save phuysmans/37b01318823904847a01d019cb154b1a to your computer and use it in GitHub Desktop.
write some bytes to a file in golang
package main
import (
"fmt"
"os"
"encoding/binary"
)
func WritePPMHeader(outf *os.File) {
fheader := []byte("P6\n")
binary.Write(outf,binary.LittleEndian,fheader)
fheader2 := []byte("640 480\n")
binary.Write(outf,binary.LittleEndian,fheader2)
fheader3 := []byte("255\n")
binary.Write(outf,binary.LittleEndian,fheader3)
}
func main() {
outf,_ := os.Create("junk.bin")
WritePPMHeader(outf)
for i :=0;i<50;i++ {
var ii uint8 = uint8(i)
err := binary.Write(outf, binary.LittleEndian, ii)
if err != nil {
fmt.Println("err!",err)
}
}
outf.Close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment