What is the golang equivalent of the following C code ?
fwrite(&E, sizeof(struct emp), n, f);
I tried using
[]byte(i)
to convert it, but that won't work, it seems.
You can use "encoding/binary" package:
import "encoding/binary"
func dump() {
f, err := os.Create("file.bin")
if err != nil {
log.Fatal("Couldn't open file")
}
defer f.Close()
var data = struct {
n1 uint16
n2 uint8
n3 uint8
}{1200, 2, 4}
err = binary.Write(f, binary.LittleEndian, data)
if err != nil {
log.Fatal("Write failed")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With