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