Hacker News new | past | comments | ask | show | jobs | submit login

Go is NOT C-like. The same semantics could have been achieved by making minimal changes to the existing C syntax. For me, Go seems to be suffering from the NIH syndrome -- they made many syntax and cosmetic changes to C just for the sake of change itself. (Using {} for compound statements is not enough to qualify the language as 'c-like'.)

I have no doubts that Go authors think that their syntax is superior, but they'll have a hard time convincing me that

  switch nr, er := f.Read(buf[:]); true {
is understandable (snippet taken from Go tutorial).



It is perfectly understandable from my perspective, but the caveat is that I'm used to Go's syntax and idioms and they are quite different from the usual. It has taken a while to unlearn the old way to be honest but I prefer it like this now.


Can you explain the " ; true" part?


Straight from the tutorial:

"Since the switch value is just true, we could leave it off—as is also the situation in a for statement, a missing value means true. In fact, such a switch is a form of if-else chain. While we're here, it should be mentioned that in switch statements each case has an implicit break."

The basic outcome is that:

1. The assignment to er, nr is an initialization statement for the switch.

2. The true (default value if not specified) is used to configure the switch as an if-else chain which is required as the assignment above makes the purpose of the switch ambiguous (is the result of the assignment configuring the switch - how do you do that as multiple values are returned?).

You could rewrite it:

    nr, er := f.Read(buf[:]);
    switch true {
      ...
    }
or even:

    nr, er := f.Read(buf[:]);
    switch {
      ...
    }
But the switch initializer scopes it to the switch block cleanly.


"switch true {...}" or "switch {...}"? really? So how would

  switch 3.141592654 { ... }
affect the case-statements inside?


When you say "switch <value>", it matches the "case" statements based on if the value after "case" is equal to <value>. So, if you say

    const P = 3.141
    switch 3.141 {
       case P: 
          fmt.Println("This prints") 
    }
When no value is specified after "switch", the value of "true" is implied, which is why you can do:

    switch {
       case a && b: ..
       case something():
    }


No idea as I wouldn't do it :) (sorry - cop out that)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: