On Tue, Jun 22, 2021 at 5:40 PM Vaibhav Maurya <vaibhav.se...@gmail.com> wrote:

> https://golang.org/ref/spec#Composite_literals
>
> following is the search string for CTRL + F
> // vowels[ch] is true if ch is a vowel \
>
> Following declaration and initialization is confusing.
> vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 
> 'y': true}
>
> Here one can see the vowels is an array. Where in the array initialization 
> syntax, there is a key value pair. I believe bool is the primitive type, so 
> the array values should be either true or false.
> Why there are key value pair separated by colon in the initialization.

The key in, for example 'a': true is 'a'. 'a' is an untyped integer
literal havine value 0x61, ie. 97. So the example key is the same as
97: true. The meaning of such key/pair in the initializer of a
[128]bool typed value is to set index 97 to true.

This is specified, quoting from the link you provided, as:

""""
The key is interpreted as a field name for struct literals, an index
for array and slice literals, and a key for map literals.
""""

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-VJOVMJ1ZguEyNjK%3DSpq4%2B0HOk%3DTC-oqW15EgSg8UgryQ%40mail.gmail.com.

Reply via email to