90

We can easily do that with maps:

item, ok := myMap["index"]

But not with slices:

item, ok := mySlice[3] // panic!

Surprised this wasn't asked before. Maybe I'm on the wrong mental model with Go slices?

marcio
  • 10,002
  • 11
  • 54
  • 83
  • 54
    why so many downvoters? You can't make any question anymore without somebody getting unhappy here. – marcio Dec 02 '14 at 14:53
  • 4
    I can't see why either. It's not unreasonable to think that maps and slices would work the same, just like std::map and std::vector have a similar interface in C++. – laurent Dec 02 '14 at 15:00
  • 6
    I (wrongly) assumed Go slices were basically maps with consecutive int keys. A quick look on the syntax can make you adopt this mental model, BTW. Still don't get why the desire to downvote a question because of that, though. Makes me wonder if Go community is being invaded by passive aggressive CI students or something LOL – marcio Dec 02 '14 at 15:06
  • 2
    "shows little effort" this is quite relative from person to person. If you follow this criteria you would need to downvote 80% of [go] questions because it's on the go tour or docs or language specs: http://stackoverflow.com/questions/2050391/how-to-test-key-existence-in-a-map – marcio Dec 02 '14 at 15:13
  • true (unfortunately, many of them do). That also may have recently changed and wasn't well documented in 2010, because the first release candidate didn't come out until 2011. – JimB Dec 02 '14 at 15:16
  • 2
    I couldn't find any information explaining explicitly that you need to check length of a slice anywere. IMMO it would be really good to have a more consistent interface between maps and slices. Anyway, thanks to stack overflow, the question is answered. – marcio Dec 02 '14 at 15:18
  • 1
    @marioAlmada It's even worse, if you create a program that uses variable amount of arguments (for example -? or -help or -help debug), you must check the length of the arguments every time. – Dippo Dec 02 '14 at 19:59
  • 3
    @marcioAlmada please stop guessing. APL syntax and semantics do not work in Java, C++ semantics don't apply to to Lisp and whatever-other-language is not a suitable reference to Go. I really can recommend the Go Tour, Effective Go and the language reference. E.g. out of rangle slice access is clearly described in http://golang.org/ref/spec#Index_expressions . – Volker Dec 02 '14 at 21:15
  • @Dippo totally agree, it's the first thing I really dislike in Go, for now. – marcio Dec 02 '14 at 22:49
  • 2
    @Volker no way, "guess" is half the fun of discovery, specially when you guess right. – marcio Dec 02 '14 at 22:51

1 Answers1

78

There is no sparse slices in Go, so you could simply check the length:

if len(mySlice) > 3 {
    // ...
}

If the length is greater than 3, you know that the index 3 and all those before that exist.

laurent
  • 88,262
  • 77
  • 290
  • 428
  • So to know if I can get `mySlice[3]` the slice needs to have index 4? That's weird. What if index 3 is the last one? – marcio Dec 02 '14 at 14:54
  • 15
    Indexes start at 0, so you need a length of 4 if you want something at index 3. – laurent Dec 02 '14 at 14:58
  • 1
    All right, it seems I understood Go slices wrong. I thought it was basically a map with consecutive int keys. A quick look on the syntax can make you adopt this mental model :) – marcio Dec 02 '14 at 15:03
  • 3
    @marcioAlmada sounds like you're coming from javascript / php. – OneOfOne Dec 02 '14 at 15:42
  • 1
    @OneOfOne Now you're the one making wrong assumptions :) I work with C++, Lua, Red, Python, PHP, Javascript and a bit of Ruby (don't like ruby). – marcio Dec 02 '14 at 22:48
  • @marcioAlmada you did mention php and javascript ;) hence the whole array-as-object thing – OneOfOne Dec 02 '14 at 23:25
  • Not only with PHP and JS, Lua and C++ baggage can make you think like this too. Anyway, it's a shame Go slices have no better way to check for an index, it totally breaks code consistency with this `if len(thing) > i { ... }` idea opposed to `item, ok := thing[i]`. – marcio Dec 02 '14 at 23:47
  • 3
    yeah golang clearly wasn't developed with the programmer in mind – Sentinel Jul 02 '19 at 13:05