Hacker News new | past | comments | ask | show | jobs | submit login

> In Python you are allowed to use tuples of immutable objects as keys

You can do this in Go too with structs.




Don't all the keys in a Go map have to have the same type? So if you use structs as keys, they all need to have the same length?

In Python, you can use (1, 2), (3, 4, 5) and (6, 7, 8, 9, 10) all as keys in the same map.


Same in Go, you can use interface{} values as keys, the concrete values of the interface keys can be a 2-element, 3-element or n-element array values.

    package main
    import "fmt"
    
    func main() {
        var m = map[interface{}]int {
            [2]int{1, 2}: 12,
            [3]int{1, 2, 4}: 123,
            [4]int{1, 2, 3, 4}: 1234,
        }
        fmt.Println(m)
    }


Oh, cool! This still doesn't quite get rid of my complaint since the size of an array must be known at compile time, but it's good that they don't all have to be the same size.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: