I'm trying to convert an uint32 to a byte array (4 bytes) in Go using the unsafe library:
h := (uint32)(((fh.year*100+fh.month)*100+fh.day)*100 + fh.h)
a := make([]byte, unsafe.Sizeof(h))
copy(a, *(*[]byte)(unsafe.Pointer(&h)))
The first two lines are correct, but then I get a runtime error ( unexpected fault address ) at the copy call.
The next step would be to call Write
_, err = fi.Write(a)
to write the 4 bytes into a file.
I've found other questions with a similar topic, but none with a working code. I'm also aware that unsafe is unsafe.
Any help would be greatly appreciated.