Skip to content

proposal: Go 2: make []byte comparable (more accurately, reflect its inherent comparability) #38377

Closed
@a3s7p

Description

@a3s7p

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.

  1. Using bytes.Equal instead of == for comparing byte slices is merely a syntactic nuisance.
  2. So is using byte slices as keys in a map via string(b).
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeLanguageChangeSuggested changes to the Go languageProposalWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.v2An incompatible library change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions