title: Reading bytes into structs using reflection
body: <p>I'm trying to write functions that will allow me to marshal/unmarshal simple structs into byte arrays. I've succeeded in writing <code>Marshal</code>, with help from the kind folks at #go-nuts, but I'm running into trouble writing <code>Unmarshal</code>.</p>
  
  <pre><code>// Unmarshal unpacks the binary data and stores it in the packet using
  // reflection.
  func Unmarshal(b []byte, t reflect.Type) (pkt interface{}, err error) {
      buf := bytes.NewBuffer(b)
      p := reflect.New(t)
      v := reflect.ValueOf(p)
      for i := 0; i &lt; t.NumField(); i++ {
          f := v.Field(i)
          switch f.Kind() {
          case reflect.String:
              // length of string
              var l int16
              var e error
              e = binary.Read(buf, binary.BigEndian, &amp;l)
              if e != nil {
                  err = e
                  return
              }
  
              // read length-of-string bytes from the buffer
              raw := make([]byte, l)
              _, e = buf.Read(raw)
              if e != nil {
                  err = e
                  return
              }
  
              // convert the bytes to a string
              f.SetString(bytes.NewBuffer(raw).String())
          default:
              e := binary.Read(buf, binary.BigEndian, f.Addr())
              if e != nil {
                  err = e
                  return
              }
          }
      }
  
      pkt = p
      return
  }
  </code></pre>
  
  <p>The problem with the code above is that the call to <code>f.Addr()</code> near the end is apparently trying to get the address of an unaddressable value.</p>
  
  <p>If there is an alternative solution, I would appreciate that as well. Either way, any help would be much appreciated.</p>
  
  <p>Thanks!</p>