Home  >  Article  >  Backend Development  >  Access specific fields of a generic structure passed to a variadic function

Access specific fields of a generic structure passed to a variadic function

WBOY
WBOYforward
2024-02-15 15:42:231023browse

Access specific fields of a generic structure passed to a variadic function

php editor Strawberry introduces you to a general structure of a specific field, which is used to pass access to variable parameter functions. A variadic function is a function that accepts a variable number of parameters, but in practical applications, we often need to pass the values ​​of specific fields to these functions. With this general structure, we can easily pass access to variadic functions and specify the value of a specific field, allowing for more flexible and precise parameter passing. This common structure can greatly simplify our code and improve its readability and maintainability.

Question content

Suppose I have a generic struct named foo:

type foo[t any] struct {
    data t
}

I have a variadic function and I want to pass some foos to it. They can be of any type foo. My understanding is that because foo[int] is not the same as foo[string], I need to define the ellipsis as type any like this:

func bar(things ...any) {
    for _, v := range things {
        fmt.println(v)
    }
}

This actually works.

func main() {
    a := foo[string]{"cheese"}
    b := foo[int]{42}

    bar(a, b)
}

My problem is that I want to specifically access the data fields in each foo. But if I define bar like this,

func bar(things ...any) {
    for _, v := range things {
        fmt.println(v.data)
    }
}

The compiler will understandably get upset, because things could be anything, so they are not guaranteed to have data fields.

I know there will be a field called data because I always pass foos, but I can't specify that I only pass foos, like this,

func bar(things ...foo) {
    for _, v := range things {
        fmt.Println(v.data)
    }
}

Because the type of foo is not specified.

How do I pass an unspecified amount of foo to bar and then access the data field?

Workaround

One solution seems to be to use reflection and access the field using fieldbyname:

func bar(things ...any) {
    for _, v := range things {
        x := reflect.ValueOf(v)
        fmt.Println(x.FieldByName("data"))
    }
}

I don't know if this would be considered a hindrance and/or danger for some reason. Presumably I need to do some checking in there to make sure everything in things is actually some kind of foo , otherwise I might be trying to access a field that doesn't exist.

The above is the detailed content of Access specific fields of a generic structure passed to a variadic function. 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
Previous article:Date format in GoNext article:Date format in Go