我真的不知道为什么switch t := some.(type){}
效果很好,但是如果我尝试了switch k := f.Kind(){}
等等。
.\mym.go:58: k := f.Kind() used as value
exit status 2
答案 0 :(得分:3)
是的你是对的,这是语法错误;它应该是SimpleStmt或
ExprSwitchStmt ="开关" [SimpleStmt&#34 ;;" ] [表达]" {" {ExprCaseClause}"}" 。
看到:
https://golang.org/ref/spec#Switch_statements
在表达式切换中,这些个案包含与switch表达式的值进行比较的表达式。
这将有效:
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!")
}
}