This isn't a bug. You're trying to assign a slice value to an array
variable. The language doesn't allow converting a string or a slice to
an array.
Why do you need to store a string in an array instead of a slice?
- Evan
That works, but I'm not sure it's actually supposed to. The spec
doesn't say anything about it that I can find.
- Evan
Right, I understand what [...] means. I'm just not sure that
[...]byte(...) is actually an array literal. It looks more like a
conversion where the value happens to be statically known.
- Evan
Yes, but what I'm saying is that:
[...]byte("hello world")
is not a composite literal. It's a conversion. A composite literal would be:
[...]byte{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}
- Evan
It's possible to cast a struct to a []byte with package unsafe, but,
well, it's unsafe. :) Another possibility is using the encoding/binary
package, although it's quite a bit slower than the unsafe way.
- Evan
Also, if you don't care about having a fixed width encoding, you can
use gob. It's type safe.
I'd say just copy the string contents into it... However, if you want to to convert a string into 20-byte array without thinking about it, it's most likely a bug. If my username is "Дмитрий Честных" and you store it as 20 bytes, my name would be "Дмитрий Че�". You just can't cut it to exactly 20 bytes. Probably, you'd cut it to "Дмитрий Че" (19 bytes), and 20th byte would be some filler (like 0). In any case, you have to do the conversion manually to get the correct result.-Dmitry
Sorry, I misread. You'd have to do something like: http://goo.gl/YWin7
Sorry, I misread. You'd have to do something like: http://goo.gl/YWin7