There are some dangerous misconceptions in this analysis.
Go is a strongly and explicitly typed language. There is no such thing as an undefined value and there is certainly no such thing as a numeric context. Instead, every variable has a well-defined, fixed type, specified (or inferred) when the variable is declared, and every type has a well-defined zero value, that is, any variable not explicitly assigned to will be initialized to the zero value of its type.
Let's look at the first line then:
- It declares a map, specifying explicitly that the keys are strings (which is a separate, pre-defined type in Go) and the values are integers. Using anything else than strings as keys or treating the values as anything other than integers would result in a compile time error.
- It initializes the map, allocating the necessary internal data structures and returning the result. The zero value of any map type is such an empty, ready-to-use map.
- It assigns this empty, initialized map to counts, using the := operator which declares a variable using type inference. The compiler knows that counts has to be a map[string]int, so you don't have to write it twice.
In turn, the counts[input.Text()]++ line can be analyzed as follows:
- Attempting to read the value for a key that does not have an entry in the map will result in the zero value of the value type, in this case 0. Not because there is any kind of numeric context anywhere, but because the map's value type is integer.
- Elements can be added to the map with assignment operations.
- x++ is not an expression in Go, and you can't use it as such; it is a statement that can be thought as the atomic equivalent of x=x+1, and as such, it is an assigment.
It is likely that the authors of go recognized the code in question as a common idiom and wrote the language standard with it in mind, but the details explained above make it clear that the underlying concepts are rather different from those of Perl.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.