我在 Go 中使用类型开关,例如以下一个:switch question.(type) {case interfaces.ComputedQuestion: handleComputedQuestion(question.(interfaces.ComputedQuestion), symbols)case interfaces.InputQuestion: handleInputQuestion(question.(interfaces.InputQuestion), symbols)}有没有办法防止我必须在案例中声明问题类型,然后才能将其传递给另一个函数?
1 回答

慕标5832272
TA贡献1966条经验 获得超4个赞
是的,分配类型开关的结果会给你断言的类型
switch question := question.(type) {
case interfaces.ComputedQuestion:
handleComputedQuestion(question, symbols)
case interfaces.InputQuestion:
handleInputQuestion(question, symbols)
}
http://play.golang.org/p/qy0TPhypvp
- 1 回答
- 0 关注
- 166 浏览
添加回答
举报
0/150
提交
取消