idiomatic "map contains" in a switch/case statement?

4,655 views
Skip to first unread message

Steven Johnson

unread,
Nov 7, 2012, 2:30:29 PM11/7/12
to golang-nuts
If I want to do a test of "does this map contain this entry", it's
simple enough to do in an if statement:

m := map[string]int{"foo":1}
if _, ok := m["foo"]; ok {
fmt.Println("map contains foo")
}

But similar syntax doesn't work in switch:

m := map[string]int{"foo":1}
switch {
case _, ok := m["foo"]; ok:
fmt.Println("map contains foo")
}

Is there any baked-in way to do such a test in switch?

I can of course use a helper function, but that's kinda awkward:

func contains(m map[string]int, s string) bool {
_, ok := m[s]
return ok
}

m := map[string]int{"foo":1}
switch {
case contains(m, "foo"):
fmt.Println("map contains foo")
}

Rob Pike

unread,
Nov 7, 2012, 2:37:46 PM11/7/12
to Steven Johnson, golang-nuts
If you know there will never be a non-zero element for a key present
in the map, you could do

switch {
case m["wassup"] != 0:
etc()
}

If 0 is a valid element, that won't work. But don't you dis helper
functions. They help. And they're functional.

-rob

Steven Johnson

unread,
Nov 7, 2012, 2:39:55 PM11/7/12
to Rob Pike, golang-nuts
On Wed, Nov 7, 2012 at 11:37 AM, Rob Pike <r...@golang.org> wrote:
> If you know there will never be a non-zero element for a key present
> in the map, you could do

Won't help in this case, alas.

> But don't you dis helper functions. They help. And they're functional.

As a general rule, sure. But why can't we have the if-syntax also work
for case-syntax? Is there a reason I'm overlooking, or was it just an
oversight?

minux

unread,
Nov 7, 2012, 2:42:27 PM11/7/12
to Steven Johnson, Rob Pike, golang-nuts
On Thu, Nov 8, 2012 at 3:39 AM, Steven Johnson <s...@google.com> wrote:
> But don't you dis helper functions. They help. And they're functional.

As a general rule, sure. But why can't we have the if-syntax also work
for case-syntax? Is there a reason I'm overlooking, or was it just an
oversight?
if you want that, why not just use nested if?

switch true is only a shorthand for one frequently used form of nested if
sequence (and the same trick is even used in good old C).

Steven Johnson

unread,
Nov 7, 2012, 2:45:41 PM11/7/12
to minux, Rob Pike, golang-nuts
On Wed, Nov 7, 2012 at 11:42 AM, minux <minu...@gmail.com> wrote:
> if you want that, why not just use nested if?

Probably what I'll end up doing. Was curious because it seems that
idiomatic Go seems to prefer switch to repeated if-else statements;
this syntactic nit makes it infeasible to do an exact equivalent in
these cases.

jorelli

unread,
Nov 7, 2012, 5:34:49 PM11/7/12
to golan...@googlegroups.com
m := map[string]int{"foo":1} 
f := func(key string) bool { _, ok := m[key]; return ok }
switch {
    case f(key):
        // whatever

Steven Johnson

unread,
Nov 7, 2012, 6:17:22 PM11/7/12
to jorelli, golan...@googlegroups.com
On Wed, Nov 7, 2012 at 2:34 PM, jorelli <jordan...@gmail.com> wrote:
> m := map[string]int{"foo":1}
> f := func(key string) bool { _, ok := m[key]; return ok }

Or, eliminating the middleman:

m := map[string]int{"foo":1}
switch {
case func()bool{ _, ok := m["foo"]; return ok}():
fmt.Println("map contains foo")
}

Don't think I want to go down that particular road, though...
Reply all
Reply to author
Forward
0 new messages