I think it is well described as "short-circuit evaluation". It is common in 
almost every popular programing languages (as far as I can tell).
In short, it means in `true || A()` A is never called and in `false && B()` 
B is never called.

It is the reason you can write something like ` if n<len(a) && a[n]==3 ` 
without having a panick of range error even when you manually test against 
it.

dc0d於 2017年12月31日星期日 UTC+8下午4時01分44秒寫道:
>
> Thanks! 
>
> Still it's the opposite of how a case works inside a switch statement. 
>
> I understand these are different contexts but this difference is a bit of 
> inconsistency and an unnecessary cognitive load (IMHO).
>
> func main() {
>  switch {
>  case false && first() == 1:
>  case true:
>  log.Println("first not called")
>  }
>
>
>  // Output:
>  // first not called
> }
>
>
> func first() int {
>  defer log.Println("first called")
>  return 1
> }
>
>
>
> On Sunday, December 31, 2017 at 11:21:57 AM UTC+3:30, Silviu Capota Mera 
> wrote:
>>
>> Hi,
>>
>> I think your answer can be found in this section: 
>> https://golang.org/ref/spec#SendStmt
>>
>> "Both the channel and the *value expression are evaluated* before 
>> communication begins"
>>
>> cheers,
>> silviu
>>
>> On Sunday, 31 December 2017 01:44:31 UTC-5, dc0d wrote:
>>>
>>> Assume there is this select statement:
>>>
>>> for {
>>>     select {
>>>     // ...
>>>     // ...
>>>
>>>     case rcvd <- first():
>>>     }
>>> }
>>>
>>>
>>> The rcvd channel will be set to nil or a chan value, by it's own case 
>>> or other cases.
>>>
>>> If the rcvd channel is nil, that case should have no effect. But even 
>>> when rcvd channel is nil, the first() function will be called.
>>>
>>> I did not expect that. Why is that so?
>>>
>>

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