Re:The Go Programming Language, or: Why all C-like languages...
by chrisjsmith on Wednesday June 08, 2011 @07:20AM
(#2632684)
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.
[ Parent
| on HN ]
Re:The Go Programming Language, or: Why all C-like languages...
by zvrba on Wednesday June 08, 2011 @07:54AM
(#2632751)
Can you explain the " ; true" part?
Re:The Go Programming Language, or: Why all C-like languages...
by chrisjsmith on Wednesday June 08, 2011 @08:51AM
(#2632882)
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.
Re:The Go Programming Language, or: Why all C-like languages...
by zvrba on Wednesday June 08, 2011 @11:31AM
(#2633519)
"switch true {...}" or "switch {...}"? really? So how would
switch 3.141592654 { ... }
affect the case-statements inside?
Re:The Go Programming Language, or: Why all C-like languages...
by supersillyus on Wednesday June 08, 2011 @01:57PM
(#2634216)
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():
}
Re:The Go Programming Language, or: Why all C-like languages...
by chrisjsmith on Wednesday June 08, 2011 @11:37AM
(#2633556)
No idea as I wouldn't do it :) (sorry - cop out that)