Oh and also: Likewise, I think this only works for array literals; I don’t think (though > again have not tried it) that you can declare slice literals with only > selected members initialized.
Works fine too: https://play.golang.org/p/ANw54ShkTvY :) On Tue, Jun 22, 2021 at 6:41 PM Axel Wagner <axel.wagner...@googlemail.com> wrote: > (I assume with a runtime rather than a compiler error, but I haven’t tried >> it) > > > Nope, compiler catches the overflow: > https://play.golang.org/p/taorqygqxFz > > On Tue, Jun 22, 2021 at 6:39 PM David Riley <fraveyd...@gmail.com> wrote: > >> On Jun 22, 2021, at 11:39, Vaibhav Maurya <vaibhav.se...@gmail.com> >> wrote: >> >> >> Hi, >> >> Please help me to understand the following syntax mentioned in the Golang >> language specification document. >> >> 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. >> >> >> In this case, it is because the single quotes create a literal rune, >> which ultimately is an integer; this is creating an array 128 wide of >> bools, of which only the values indexed by those character values are >> initialized (everything else is the zero value, or false). >> >> This example only works for characters in the 7-bit ASCII subset of >> UTF-8; if you were to put other characters in which had rune values greater >> than 127, this would break (I assume with a runtime rather than a compiler >> error, but I haven’t tried it). Likewise, I think this only works for array >> literals; I don’t think (though again have not tried it) that you can >> declare slice literals with only selected members initialized. >> >> >> - Dave >> >> -- >> 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/2564C639-E055-4DD9-97A2-9B6061F83006%40gmail.com >> <https://groups.google.com/d/msgid/golang-nuts/2564C639-E055-4DD9-97A2-9B6061F83006%40gmail.com?utm_medium=email&utm_source=footer> >> . >> > -- 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/CAEkBMfGCvfz5nCS2zcroJb-CLTkaD81mUwnhHnQ2Hr7ZJ0uHZA%40mail.gmail.com.