in reply to Re: Does Go steal from Perl? :-)
in thread Does Go steal from Perl? :-)

Go, like Python, doesn't have such a distinction, thingvalue could be an array item, a map (dict, or hash) access, or access to a char in a string. In those languages, it's the operand that defines which operation to use

Again, no.

Go is a strongly typed language, every variable has a well-defined type associated it. You can have a nested map type c := map[string]map[string]int, in which case you (and the compiler) can know that its element c["key"] is of type map[string]int and the sub-elements of that (c["key"]["subkey"]) are of type int. Assigning a value of any other type to those keys would result in a compile time error.

So in theory, you could have autovivification in the language - but in practice you can't, you have to explicitly initialize the mid-level maps yourself, as explained by this blog post: Go maps in action.

The underlying reason for this, I think, is that Go differentiates between a nil map and an empty map. The zero value of a(n uninitialized) map is a nil map, you have to initialize yourself with make. A nil map can be safely read from, but assigning any key to it results in a runtime panic. When you declare a nested map as the example above, the mid-level maps will be nil maps, and as such, are not safe to assign to. (I consider this to be a design wart of the language.)

Replies are listed 'Best First'.
Re^3: Does Go steal from Perl? :-)
by Eily (Monsignor) on Aug 03, 2018 at 21:30 UTC

    Thanks for the clarification, it was pretty unwise of me to say so much without checking my facts more. So while Go can't have the full power of autovivification on undeclared structures, it would be conceivable when declared as nested data but undefined. It's just not the case. Not sure I'm going to put that knowledge to good use any time soon, but at least I relearned not to assume what a language dis or is like :-)