I'm using go-hdf5 and I'm hitting a problem when trying to write attributes in a loop from a map.
The attributes are created correctly (correct name and datatype) but the written value is garbage.
The same code outside of the loop works fine. I tried both the v := v
idiom and wrapping the code in a closure to capture v
but it doesn't make a difference.
Here is the gist of the code (error checking intentionally left out for clarity):
m := map[string]interface{"foo", 42}
for k, v := range m {
// [...]
v := v
attr.Write(&v, dtype)
}
The Write
method is using reflection to grab a pointer to the value and forwards it to the C library. The relevant part of the code is just:
func (s *Attribute) Write(data interface{}, dtype *Datatype) error {
v := reflect.ValueOf(data)
addr := unsafe.Pointer(v.Pointer())
return h5err(C.H5Awrite(s.id, dtype.id, addr))
}
If I replace the map by a slice of interface{}
, I get the exact same problem so my hunch is that this has to do with the binding of loop variables, but yet v := v
doesn't help so I'm not sure.
I'm quite familiar with Go, HDF5 (C library) and go-hdf5 but I'm really stuck here. Any idea?
BTW I'm using go1.5.1 darwin/amd64.