I don't know really why switch t := some.(type){}
works well, but if I tried switch k := f.Kind(){}
or so on.
.\mym.go:58: k := f.Kind() used as value
exit status 2
I don't know really why switch t := some.(type){}
works well, but if I tried switch k := f.Kind(){}
or so on.
.\mym.go:58: k := f.Kind() used as value
exit status 2
Yes you are right, it is syntax error; it should be SimpleStmt or
ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
see:
https://golang.org/ref/spec#Switch_statements
In an expression switch, the cases contain expressions that are compared against the value of the switch expression.
And this will work:
package main
import (
"fmt"
)
type Test struct {
kind int
}
func (s *Test) Kind() int {
return s.kind
}
func main() {
f := &Test{12}
//fmt.Println(k := f.Kind()) //syntax error: unexpected :=, expecting comma or )
switch k := f.Kind(); k {
case 12:
fmt.Println(k) //12
case 0:
fmt.Println("Bye!")
}
}