go - Golang改变指针接口的类型
问题描述
我有一个函数,它返回某个接口的一部分指针。我想稍后在代码中将类型更改为实现类型,但没有任何效果,我仍然得到一个无效的类型断言。
例子
func Test(c Parsable)([]*Parsable, error) {
// generate slice by factory method on Parsable inteface and return slice
}
var implParsable ImplParsable
results, err := Test(implParsable)
data := results[0].(ImplParsable) // I tried this in many variations but nothing works
解决方案
resultSets[0]
是指向接口的指针,因此您需要取消引用该指针以获取接口值,您可以内联执行此操作,因为切片值是可寻址的。
data := (*resultSets[0]).(ImplParsable)
推荐阅读
- python - 如何将 pandas loc 索引器转换为字符串?
- ssas - 计算跨不同时间维度的度量
- phone-call - 如何在 RingOut 中使用 CNAM(来电显示名称)?
- networking - NS2跟踪文件中AGT级别的重要性
- java - 如何排除已经验证的链接验证?
- haskell - 如何正确更新 Data.IORef 中保存的 Haskell 记录?
- java - JdbcTemplate BeanPropertyRowMapper 的问题 - 将 int & double 字段设置为 0 而不是实际值
- java - java检查数据类型ArrayList
- java - 使用带有连接字符分隔符的收集器
- wordpress - 移动菜单打开时删除汉堡菜单切换