我有一个静态声明的变量
var fun *ast.FunDecl
和一个名为 tyle 的数组,该数组保存不同类型的项和 .declast.Declast.GenDecl*ast.FunDecl
现在,在运行时,我想循环访问数组,并将第一个出现的项指定给我的变量。*ast.FunDeclfun
在我的数组迭代中,当前数组元素在哪里,我正在使用:d
switch 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.功能描述。
除了解决这种特殊情况之外,这还把我带到了一个普遍的问题,如何处理这样的类型转换情况?如果我知道变量的类型与我的强制转换匹配,如何将其强制转换为特定类型?
互换的青春
犯罪嫌疑人X
随时随地看视频慕课网APP
相关分类