
Go maps in action - zoowar
http://blog.golang.org/2013/02/go-maps-in-action.html
======
shadowmint
I don't really get it. It's a hash. Big deal.

I know what a hash is. Wow, Go has hashes. Yeah, I knew that too.

 _blinks_ So... yeah. I mean, it's the Go blog, so cool. Blog about things in
the language, but in terms of 'interesting things about Go' or 'interesting
things about tech I expect to see on hacker news frontpage' this is pretty
much a 0 for me...

~~~
enneff
So an article titled "Go maps in action" is about Go maps and... you're
underwhelmed? Dissatisfied? This is an article for people who want to learn
about Go maps. I'm not sure what you expected.

You would be surprised how valuable these kinds of "entry level" articles are.
I get more grateful mail about the basic articles than I do the advanced ones,
because the audience is so much wider.

So, not for you I guess.

~~~
est
reminds me of people defending reposts on reddit.

Yeah I've never seen it three minutes ago on frontpage, so the repost is OK
for me and people like me.

Maps in Go is definitely a good lesson for hackers who doesn't know about
maps...

------
omaranto
The fact that slices cannot be used as keys in maps was, for me, a fairly big
turn off.

~~~
jerf
Using a slice as a key doesn't make much sense; a slice is a fundamentally-
mutable view onto an underlying array, which itself may be mutable.

Quick testing shows that you _can_ use arrays:

    
    
        func main() {
            a := [4]int{1,2,3,4}
            m := make(map[[4]int]string)
            m[a] = "Hello"
            fmt.Println(m[[4]int{1,2,3,4}])
        }
    

Although that will be klunky, since the size of the array is in the type of
the array. And still mutable. Mutating keys is bad.

In all my years of using maps, I can't think of when I wanted to index by
something that was truly an arbitrary-length array of things. If you want to
index by something like a tuple of things, declare a datatype for that
purpose. What exactly are you trying to do by using a slice as a key?

~~~
omaranto
I was lazy when I complained about not be able to use slices as keys, I guess
I really meant to complain that there is no arbitrary-length immutable
sequence type that I can use as keys to a map (other than strings, which are
sequences of characters and are allowed as keys to a map).

For an example where you might want to do this consider storing a count of the
frequency with which different words follow a given n-gram (sequence of n
words) in some corpus, the sort of thing you'd need to make a Markov chain
nonsense text generator (like the example programs in chapter 3 of The
Practice of Programming: <http://cm.bell-labs.com/cm/cs/tpop/code.html>).

Of course, you can always take your immutable sequences and represent them as
strings, as other languages such as Perl, Awk and Lua force you to do. But I
really like that I'm not forced to use such kludges in, say, Python or
Haskell.

~~~
juiceandjuice
A map is a hash table. I'm confused as to why you don't use either a custom
hashing function or your own custom datatype that organizes your data
according to the sequence order of your key. While a generic hashing function
that can operate on any type of data could be implemented in the language, I
believe the flexibility of defining the hashing function yourself is perfectly
acceptable considering the performance implications of having a universal hash
function. The reason why is this: The table lookup should fundamentally be
roughly an O(1) operation _after_ hashing your key value. A universal hashing
function that operates on arbitrary-length immutable sequences would likely be
an additional O(n) (where n is the length of the sequence) complexity built
into the map structure. I don't believe that's acceptable from a language
standpoint.

~~~
dsymonds
There's nothing stopping you writing your own hash map that can be configured
with a custom hash function.

The builtin map is limited to types for which the language defines ==. It's a
simplifying trade-off; I don't think "acceptable" versus "unacceptable" enters
into it. One could imagine expanding the builtin map type to support a hash
function, and that was something that was considered early on, but it added
complexity that just wasn't needed most of the time.

------
gtaylor
Very helpful, thanks for posting this! The parts about map declaration vs.
make() vs. new() are the most valuable tidbits, I think. A lot of the rest is
mostly just very handy tips and pointers.

------
wei2012
Go everyday, but still going.

