Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: slice/list/map should support more operators, such as in, not in. #53067

Closed
yuezhongtao opened this issue May 25, 2022 · 1 comment
Closed

Comments

@yuezhongtao
Copy link

The current operation of built-in container types is not elegant enough. The scenarios are as follows.

  1. To determine whether an element exists in the list/slice, we need to traverse the list/slice in the business code
  2. To get the map key value set, you need to traverse the entire map in the business code
  3. Judging whether the map contains key values is not elegant enough

The solution is as follows:

  1. slice/list/map supports in and not in operators.
s := []string{"e1"}
if "e1" in s {
  //todo
}
if "e2" not in s {
  //todo
}
  1. Map supports Keys(), Values() operations
m := make(map[string]string)
keys := m.Keys()
vals := m.Values()
@gopherbot gopherbot added this to the Proposal milestone May 25, 2022
@seankhliao
Copy link
Member

The current plans look to be the following, with potential to move them into the standard library at some point (see #45955 #47649).

import "golang.org/x/exp/slices"

if slices.Contains(s, "e1") {
    // todo
}
import "golang.org/x/exp/maps"

keys := maps.Keys(m)
vals := maps.Values(m)

if "e2" not in s { is a breaking language change adding 2 new keywords, and keys := m.Keys() is also a language change adding methods to builtin types, I think their feasibility is low. As we already have concrete plans to handle these usecases, I'm going to close these here. Please comment if you disagree.

@golang golang locked and limited conversation to collaborators May 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants