Home > Backend Development > Golang > 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
Release: 2024-02-14 09:00:11
forward
777 people have browsed it

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

}
Copy after login

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")
}
Copy after login

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())
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template