Closed
Description
What version of Go are you using (go version
)?
$ go version 1.14.1
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env
)?
I don't think this is relevant.
What did you do?
I tried to use byte slices in a context where they might be compared (most notably, as fields in a struct which is a map key).
What did you expect to see?
Considering #31587, byte slices are as comparable as strings are and the comparison does not allocate. Therefore, I would expect them to be comparable like strings and acceptable as members of a struct used as a map key.
What did you see instead?
Byte slices are not comparable while strings are.
- Using
bytes.Equal
instead of==
for comparing byte slices is merely a syntactic nuisance. - So is using byte slices as keys in a map via
string(b)
. - However the current behavior means that it is not possible to use a struct containing byte slices as a map key whereas it is possible to use a struct containing strings as a map key. This leads to hacks converting one type to another or using less intuitive solutions altogether which provide no benefit over
[]byte
being comparable as-is.
It doesn't seem to me that changing this would break backwards compatibility/any code out there as currently it is not possible to use a []byte
in a comparable type context at all.
Also, the invalid map key type fnord
error message could be more helpful.