Return Dynamic Struct in Go

Asked

Viewed 76 times

1

How could a dynamic return in Go using struct ?

Example :

func teste() (*struct, err){ 
  type t struct {
  }
  return t, nil
}

The problem is that I would not like to create a struct for each method that needs to return something different. Did not want to return the complete object and wanted it in the best way, would not need to reuse this struct in any place except this, so wanted to leave it within the function.

1 answer

2


I was able to resolve it shortly 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
}

In this way I was able to return a dynamic struct, not needing to have it created outside of function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.