jeudi 7 janvier 2021

Can I alias the return type of a fuction in go?

I have a function that returns an anonymous struct. This function is generated and so I cannot change the code or create a type for the return value.

func foo() struct {
    Prop int
} {
    result := new(struct {
        Prop int
    })
    result.Prop = 1
    return *result
}

In other places in the codebase, I want to define a function, bar() that accepts as an argument the return type of foo()

func bar(arg ReturnTypeOfFoo) {
    // ...
}

Can I do this without manually defining the ReturnTypeOfFoo type? Is there some kind of reflection I can do on foo()?





Aucun commentaire:

Enregistrer un commentaire