【发布时间】:2019-01-01 10:26:48
【问题描述】:
我正在尝试将接口动态转换回其原始结构,但在转换后访问结构的属性时遇到问题。
以这段代码为例。
package main
import (
"fmt"
"log"
)
type struct1 struct {
A string
B string
}
type struct2 struct {
A string
C string
}
type struct3 struct {
A string
D string
}
func main() {
s1 := struct1{}
s1.A = "A"
structTest(s1)
s2 := struct2{}
s2.A = "A"
structTest(s2)
s3 := struct3{}
s3.A = "A"
structTest(s3)
}
func structTest(val interface{}) {
var typedVal interface{}
switch v := val.(type) {
case struct1:
fmt.Println("val is struct1")
case struct2:
fmt.Println("val is struct2")
case struct3:
fmt.Println("val is struct3")
default:
log.Panic("not sure what val is.")
}
fmt.Println(typedVal.A)
}
我希望能够将 3 种已知结构类型之一传入我的函数。然后找出传入的结构类型来键入断言它。最后,我希望能够访问类似的属性。
基本上我想在我的结构中有一些基本的继承,但到目前为止似乎不可能在 go 中做到这一点。我看到一些帖子提到使用接口进行继承,但我的结构没有方法,所以我不确定如何使用接口。
这样的事情在go中可能吗?
【问题讨论】:
-
为什么不从函数中返回值。你这样做是正确的,只需返回值以获取结构,然后根据需要使用它。
-
Go 中没有继承(我不确定“使用接口的继承”是什么意思,因为接口实现了一种多态形式)。如果您正在寻找继承,那么您就是在尝试使用错误的工具来解决问题。
-
@Himanshu 这是我正在尝试做的一个简化示例。我想我可以获取这些值并将它们返回到地图或其他东西中。当我从每个结构中获取相同的东西但我必须为每个开关选项复制粘贴它时,这似乎很浪费。
-
@jhall1990 如果不知道接口可以包含什么类型然后使用 switch 来检查,这是不可能检查接口的底层类型的。因此,您可以使用键作为名称和值作为结构的映射。这样,您将根据键了解结构并将其传递给函数。
-
如果这不是您真正的用例,那是什么。看来我误解了您的问题,您能否详细说明一下以便我们能够更好地为您提供帮助?
标签: go