我有一个静态声明的变量var fun *ast.FunDecl和一个名为 tyle 的数组,该数组保存不同类型的项和 .declast.Declast.GenDecl*ast.FunDecl现在,在运行时,我想循环访问数组,并将第一个出现的项指定给我的变量。*ast.FunDeclfun在我的数组迭代中,当前数组元素在哪里,我正在使用:dswitch t := d.(type){ case *ast.FunDecl: { fun = d // cannot use d (variable of type ast.Decl) as *ast.FuncDecl value in assignment } // more type cases ...}另外,尝试使用显式强制转换fun = *ast.FunDecl(d)恐慌说:无法转换 d(ast 类型的变量。Decl) to ast.功能描述。除了解决这种特殊情况之外,这还把我带到了一个普遍的问题,如何处理这样的类型转换情况?如果我知道变量的类型与我的强制转换匹配,如何将其强制转换为特定类型?
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报
0/150
提交
取消
