title: Go conversion between struct and byte array
body: <p>I am writing a client - server application in Go. I want to perform C-like type casting in Go.</p>
  
  <p>E.g. in Go</p>
  
  <pre><code>type packet struct {
      opcode uint16
      data [1024]byte
  }
  
  var pkt1 packet
  ...
  n, raddr, err := conn.ReadFromUDP(pkt1)  // error here
  </code></pre>
  
  <p>Also I want to perform C-like memcpy(), which will allow me to directly map the network byte stream received to a struct.</p>
  
  <p>e.g. with above received pkt1</p>
  
  <pre><code>type file_info struct {
      file_size uint32       // 4 bytes
      file_name [1020]byte
  }
  
  var file file_info
  if (pkt1.opcode == WRITE) {
      memcpy(&amp;file, pkt1.data, 1024)
  }
  </code></pre>