4 ms·
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 h
by chrisjsmith 15y ago
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.
- zvrba 15y agoCan you explain the " ; true" part?
- chrisjsmith 15y agoStraight 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.
- zvrba 15y ago"switch true {...}" or "switch {...}"? really? So how would switch 3.141592654 { ... } affect the case-statements inside?
- chrisjsmith 15y agoNo idea as I wouldn't do it :) (sorry - cop out that)
- supersillyus 15y agoWhen 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(): }