Home > Backend Development > Golang > 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
Release: 2024-02-15 15:42:23
forward
1100 people have browsed it

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

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

This actually works.

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

    bar(a, b)
}
Copy after login

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

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

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

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!

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