Return dynamic Struct in Go
How could I have a dynamic return in Go using struct ?
Example:
func teste() (*struct, err){
type t struct {
}
return t, nil
}
The problem is that I wouldn't want to create a struct for each method that needs to return something different. I didn't want to return the complete object and I wanted it in the best way, I wouldn't need to reuse this struct in any location except this one, so I wanted to leave it inside func.
1
Author: Mateus Veloso, 2020-01-27
1 answers
I was able to solve in a short time after asking the question. I'll leave the answer here to help the community.
Example of the code that would solve this situation.
func teste() (struct{name string},error){
t := struct {
name string
}{
name:"Mateus",
}
return t, nil
}
This way I was able to return a dynamic struct, not having to have it created outside of func.
2
Author: Mateus Veloso, 2020-01-27 14:29:37