Additionally, initializing data to len(u.DeviceIds) is probably not
helpful. If there are 20 deviceids, with an average individual length of,
say, 10, then that's ~200 bytes (though in this case, you would have
allocated 20 bytes), and because you're using the 2-arg form of make,
you're setting the initial _len_ not the initial cap of data, and append
only fills in the amount of cap that extends beyond the len. What this
means, is is you have 20 device ids, then your resulting data slice will
start with 20 null bytes.
If you can reasonably determine the capacity you need, do:
data := make([]byte, 0, neededCap)
Otherwise, don't worry about it, and just don't initialize data at all:
var data []byte
append will always allocate more space (for the new slice it returns) as
needed.
On Saturday, December 15, 2012 4:58:23 PM UTC-7, Moddus wrote:
Hi,
I am now trying for some time to convert a slice of strings in a slice of
bytes but it still not work.
I guess i need some help ...
data := make([]byte, len(u.DeviceIds))
for _, deviceId := range u.DeviceIds {
data = append(data, []byte(deviceId))
}
This is my idea ... compiling fails and it not working ...
Greets,
Moddus
--