Home  >  Article  >  Backend Development  >  How to access fields of nested structure in array of interface type

How to access fields of nested structure in array of interface type

WBOY
WBOYforward
2024-02-14 09:00:11704browse

How to access fields of nested structure in array of interface type

php editor Youzi will explain to you how to access fields of nested structures in interface type arrays. When processing the data returned by the interface, sometimes you will encounter nested structures, that is, the array contains deeper fields. To access these nested fields, we can use the dot operator or array subscript to obtain them layer by layer. By understanding the hierarchical structure of the array and the corresponding key names, we can easily access the required field values ​​and achieve flexible processing of the data returned by the interface. Next, we will introduce in detail how to operate nested structure fields in interface type arrays, allowing you to easily deal with various data processing scenarios.

Question content

I want to access the FieldBase fields in these nested structures.

This is my code example:

type InterfaceA interface {
    FunA()
}

type BaseStruct struct {
    FieldBase string
}

type SubStruct struct {
    BaseStruct
}

func (c SubStruct) FunA() {
}

type SubStruct2 struct {
    BaseStruct
}

func (c SubStruct2) FunA() {
}

func main() {
    var x = [2]InterfaceA{
        SubStruct{BaseStruct: BaseStruct{FieldBase: "aaa"}},
        SubStruct2{BaseStruct: BaseStruct{FieldBase: "bbb"}},
    }

    // TODO: Access fields of nested classes in the array

}

I want to know how to access the FieldBase field value of each nested structure in an array x, where x is the interface type. I tried using type assertions for access, but I can only try it on a single element.

if subStruct, ok := x[1].(SubStruct); ok {
    fmt.Println(subStruct.FieldBase)
} else {
    fmt.Println("Cannot access FieldBase")
}

Workaround

Since your array belongs to an interface, you either need a type assertion and handle for each type, or an interface method. However, I think what you want and need is to have an interface method for each struct type that exposes FieldBase , like this:

package main

import "fmt"

type InterfaceA interface {
    FunA()
    GetFieldBase() string
}

type BaseStruct struct {
    FieldBase string
}

type SubStruct struct {
    BaseStruct
}

func (c SubStruct) FunA() {
}

func (c SubStruct) GetFieldBase() string {
    return c.FieldBase
}

type SubStruct2 struct {
    BaseStruct
}

func (c SubStruct2) FunA() {
}

func (c SubStruct2) GetFieldBase() string {
    return c.FieldBase
}

func main() {
    var x = [2]InterfaceA{
        SubStruct{BaseStruct: BaseStruct{FieldBase: "aaa"}},
        SubStruct2{BaseStruct: BaseStruct{FieldBase: "bbb"}},
    }

    // TODO: Access fields of nested classes in the array

    fmt.Println(x[0].GetFieldBase())
    fmt.Println(x[1].GetFieldBase())
}

The above is the detailed content of How to access fields of nested structure in array of interface type. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete