[英]i want to access child node data using parent node name flutter firebase data base
[英]how can i get child struct name in parent func?
如何在父函數中獲取子結構名稱? 我的在線代碼在這里: https://go.dev/play/p/04w5mK0aAgL
type IParent interface {
TypeName() string
}
type Parent struct{ IParent }
func (p *Parent) TypeName() string {
if t := reflect.TypeOf(p); t.Kind() == reflect.Ptr {
return "*" + t.Elem().Name()
} else {
return t.Name()
}
}
type Child struct {
*Parent
}
func main() {
var e IParent = &Child{}
// output: TypeName: *Parent.
// expected: TypeName: *Child.
val := e.TypeName()
fmt.Printf("TypeName: %v.\n", val)
}
output 是TypeName: *Parent.
,我預期的 output 是TypeName: *Child.
,有人可以給我一些建議嗎?
我想你想念 go 中的結構概念。
type Child struct {
*Parent
}
func main() {
var e IParent = &Child{}
上面,很明顯,如果你定義了帶有 Parent 的 struct Child 作為 struct 的內容,但之后你聲明了 child 和 output 的內容。 在這里很明顯,結果是 *Parent。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.