0
type Person struct {
    Example []struct {
        Firstname string `json:"firstname"`
        Lastname  string `json:"lastname"`
        Address   []struct {
            Street  string `json:"street"`
            City    string `json:"city"`
            Zip     string `json:"zip"`
            Country string `json:"country"`
        } `json:"address"`
        Age string `json:"age"`
    }  `json:"users"`
}

I am making a function in go that is going to take a name and I'm going to iterate through my json file that has multiple people and return the person that matches the name that is passed. However, for my purposes, I need to return the Example struct, and I do not know how to specify the return type to be an Example struct. I would prefer if I did not have to separate into multiple structures as I'm doing a variety of tasks with the aforementioned code.

RickyA
  • 15,465
  • 5
  • 71
  • 95
Rohit Panikar
  • 85
  • 2
  • 2
  • 5
  • 1
    Possible duplicate of [How to initialise nested structs in go?](https://stackoverflow.com/questions/30102932/how-to-initialise-nested-structs-in-go/30104985#30104985) – icza May 25 '17 at 11:27
  • In your example `Example` is not a `struct`, it's a field whose type is a `slice` of *anonymous* structs, therefore `Example` is not a `type` you can return. If you don't want to "name" your type you will have to repeat yourself when specifying the return type of your function like so https://play.golang.org/p/BTBGRqyrMl. This is, of course, inefficient so just "separate" your `Person` into multiple types. – mkopriva May 25 '17 at 11:35
  • Ok, I understand what you're saying! After looking at my code again, I think I'll separate the structs since that's more like the "go way". Thanks! – Rohit Panikar May 25 '17 at 20:19

0 Answers0