Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-23 Thread jake...@gmail.com
No that is exactly what I meant. I would never use it, as it seems like 
obfuscation, but there are those who like to be clever.

On Tuesday, June 22, 2021 at 5:36:56 PM UTC-4 Rob 'Commander' Pike wrote:

> That creates a slice 101 integers long, which probably isn't what you 
> meant, which might help explain why you never came across it before.
>
> Smile.
>
> -rob
>
>
> On Wed, Jun 23, 2021 at 7:07 AM jake...@gmail.com  
> wrote:
>
>> I'm surprised that I have never come across this as a way to create a 
>> slice with an initial length:
>>
>> x := []int{100:0}
>>
>> On Tuesday, June 22, 2021 at 12:43:17 PM UTC-4 axel.wa...@googlemail.com 
>> wrote:
>>
>>> 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  
>>> 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  wrote:

> On Jun 22, 2021, at 11:39, Vaibhav Maurya  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...@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
>  
> 
> .
>
 -- 
>> 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...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/7d83fac1-3db0-4905-85e6-cc14b8c74389n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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/2d8227ab-4053-4df4-a69b-40ba42c1645en%40googlegroups.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread Jan Mercl
On Wed, Jun 23, 2021 at 7:06 AM Vaibhav Maurya  wrote:

> I appreciate the whole discussion, it was really insightful. Since Golang is 
> not my first language, I had a preconceived notion about key-value pair 
> initialization.
> Here I would like to disagree with the syntax, it would be good that with the 
> innovation in the language, the general idea of an Array and slice should be 
> maintained, instead of making certain syntaxes so weird.

The composite literal syntax is in essence the same as what modern C
has for decades. Not sure what's weird about it.

Can you elaborate on "it would be good that with the innovation in the
language, the general idea of an Array and slice should be maintained"
or provide some examples of what you are thinking about?

> This syntax is really unintuitive for me. the key-value pair structures give 
> so many pictures like hashing, scattered memory allocations, etc.

It's obviously subjective, but I wonder, how would the syntax, that
you would call intuitive, look like.

It may happen that you have some nice idea that's worth turning into a
proposal for improvement.

-- 
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-UY4MXT1%3DVWE31nXouR2jJ2iQLF5MHZG%3D0_8dL9s2W9og%40mail.gmail.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread 'Axel Wagner' via golang-nuts
FWIW I don't believe I ever saw key-value initialization of arrays or
slices in the wild.
Not saying *no one* uses it, but it's certainly rare enough that I wouldn't
worry about getting confused by it in practice.

On Wed, Jun 23, 2021 at 7:07 AM Vaibhav Maurya 
wrote:

>
> I appreciate the whole discussion, it was really insightful. Since Golang
> is not my first language, I had a preconceived notion about key-value pair
> initialization.
> Here I would like to disagree with the syntax, it would be good that with
> the innovation in the language, the general idea of an Array and slice
> should be maintained, instead of making certain syntaxes so weird.
>
> This syntax is really unintuitive for me. the key-value pair structures
> give so many pictures like hashing, scattered memory allocations, etc.
>
> On Wednesday, June 23, 2021 at 3:06:56 AM UTC+5:30 Rob 'Commander' Pike
> wrote:
>
>> That creates a slice 101 integers long, which probably isn't what you
>> meant, which might help explain why you never came across it before.
>>
>> Smile.
>>
>> -rob
>>
>>
>> On Wed, Jun 23, 2021 at 7:07 AM jake...@gmail.com 
>> wrote:
>>
>>> I'm surprised that I have never come across this as a way to create a
>>> slice with an initial length:
>>>
>>> x := []int{100:0}
>>>
>>> On Tuesday, June 22, 2021 at 12:43:17 PM UTC-4 axel.wa...@googlemail.com
>>> wrote:
>>>
 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 
 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 
> wrote:
>
>> On Jun 22, 2021, at 11:39, Vaibhav Maurya 
>> 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...@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
>> 
>> .
>>
> --
>>> 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...@googlegroups.com.
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/golang-nuts/7d83fac1-3db0-4905-85e6-cc14b8c74389n%40googlegroups.com
>>> 
>>> .
>>>
>> --
> 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 go

Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread Vaibhav Maurya

I appreciate the whole discussion, it was really insightful. Since Golang 
is not my first language, I had a preconceived notion about key-value pair 
initialization.
Here I would like to disagree with the syntax, it would be good that with 
the innovation in the language, the general idea of an Array and slice 
should be maintained, instead of making certain syntaxes so weird.

This syntax is really unintuitive for me. the key-value pair structures 
give so many pictures like hashing, scattered memory allocations, etc.

On Wednesday, June 23, 2021 at 3:06:56 AM UTC+5:30 Rob 'Commander' Pike 
wrote:

> That creates a slice 101 integers long, which probably isn't what you 
> meant, which might help explain why you never came across it before.
>
> Smile.
>
> -rob
>
>
> On Wed, Jun 23, 2021 at 7:07 AM jake...@gmail.com  
> wrote:
>
>> I'm surprised that I have never come across this as a way to create a 
>> slice with an initial length:
>>
>> x := []int{100:0}
>>
>> On Tuesday, June 22, 2021 at 12:43:17 PM UTC-4 axel.wa...@googlemail.com 
>> wrote:
>>
>>> 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  
>>> 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  wrote:

> On Jun 22, 2021, at 11:39, Vaibhav Maurya  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...@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
>  
> 
> .
>
 -- 
>> 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...@googlegroups.com.
>>
> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/7d83fac1-3db0-4905-85e6-cc14b8c74389n%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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/70d14d3c-d333-41d9-945f-de9d5a028458n%40googlegroups.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread Rob Pike
That creates a slice 101 integers long, which probably isn't what you
meant, which might help explain why you never came across it before.

Smile.

-rob


On Wed, Jun 23, 2021 at 7:07 AM jake...@gmail.com 
wrote:

> I'm surprised that I have never come across this as a way to create a
> slice with an initial length:
>
> x := []int{100:0}
>
> On Tuesday, June 22, 2021 at 12:43:17 PM UTC-4 axel.wa...@googlemail.com
> wrote:
>
>> 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 
>> 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  wrote:
>>>
 On Jun 22, 2021, at 11:39, Vaibhav Maurya  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...@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
 
 .

>>> --
> 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/7d83fac1-3db0-4905-85e6-cc14b8c74389n%40googlegroups.com
> 
> .
>

-- 
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/CAOXNBZQi5_TiNv8e81H54jbvJLkM3XMyXqW_aq22Psg4oaRQAw%40mail.gmail.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread jake...@gmail.com
I'm surprised that I have never come across this as a way to create a slice 
with an initial length:

x := []int{100:0}

On Tuesday, June 22, 2021 at 12:43:17 PM UTC-4 axel.wa...@googlemail.com 
wrote:

> 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  
> 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  wrote:
>>
>>> On Jun 22, 2021, at 11:39, Vaibhav Maurya  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...@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
>>>  
>>> 
>>> .
>>>
>>

-- 
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/7d83fac1-3db0-4905-85e6-cc14b8c74389n%40googlegroups.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread Bakul Shah
If you are familiar with C99’s designated initializers, this is similar but 
less general
and less confusing.

> On Jun 22, 2021, at 8:40 AM, Vaibhav Maurya  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.
> -- 
> 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/83571d7c-7dee-47f3-b105-4286320e88d6n%40googlegroups.com.

-- 
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/E7DEE768-5F85-4AE2-BDBD-830E8DBEA199%40iitbombay.org.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread David Riley
On Jun 22, 2021, at 12:42, Axel Wagner  wrote:
> 
> 
> 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 :)

Yeah, I re-read the doc and realized my assumption was erroneous shortly after 
I sent the last one… this is why I put the disclaimer on things I have not 
tried yet. :-)


- 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/C0E9CFB3-0B02-49A8-8EE0-8CB9CA19681E%40gmail.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread 'Axel Wagner' via golang-nuts
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 
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  wrote:
>
>> On Jun 22, 2021, at 11:39, Vaibhav Maurya 
>> 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
>> 
>> .
>>
>

-- 
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.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread 'Axel Wagner' via golang-nuts
>
> (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  wrote:

> On Jun 22, 2021, at 11:39, Vaibhav Maurya  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
> 
> .
>

-- 
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/CAEkBMfGxAQEHy32157s9sWu0f1qQ3cZ%3DoLwk%2BrjyQmUnmrt1oA%40mail.gmail.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread David Riley
On Jun 22, 2021, at 11:39, Vaibhav Maurya  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.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread 'Axel Wagner' via golang-nuts
It's in the section you link to:

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


(emphasis mine). The syntax allows you to specify keys for arrays and
slices and interprets them as indices. Rune-literals (like 'a') are
integers, as `rune` is an alias for `int32`.

On Tue, Jun 22, 2021 at 5:40 PM Vaibhav Maurya 
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.
>
> --
> 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/83571d7c-7dee-47f3-b105-4286320e88d6n%40googlegroups.com
> 
> .
>

-- 
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/CAEkBMfEHUW_N%3DNMXnSG8aPuGJcpPb4erO-V451_jTts8iCyhPQ%40mail.gmail.com.


Re: [go-nuts] Golang language specification Document: Composite Literals example is not understandable

2021-06-22 Thread Jan Mercl
On Tue, Jun 22, 2021 at 5:40 PM Vaibhav Maurya  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.