* jlforr...@berkeley.edu <jlforr...@berkeley.edu> [190329 11:16]:
> I get the error
> 
> assignment mismatch: 2 variable but 1 values
> 
> for the assignment statement in Part 2 but the assignment statement in
> Part 1 compiles.  Both assignment statements have the same number of
> values, but it appears that referencing an integer in a structure
> field in the 2nd assignment statement, rather than a simple integer,
> confuses the compiler.
> ---------------------------
> 
> var a = map[int]int{
>         1: 2,
> }
> 
> var b = map[int]Word{
>         1: {2, 3},
> }
> 
>         // Part 1
>         num, ok := a[1]
> 
>         // Part 2
>         num, ok = b[1].code   // assignment mismatch: 2 variable but 1 values

To add to what others have said, look at the Go Language Specification
under Index expressions at https://golang.org/ref/spec#Index_expressions
just above the heading "Slice expressions".

It says that if a is a map, an assignment or initialization of the
special form «v, ok = a[x]» yields an additional untyped boolean.  In
Part 2 above, you are not assigning b[1] (which would work), but
b[1].code which does not match the special form whose purpose is to
allow the programmer to determine at runtime if the map contains the
given key.

...Marvin

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to