S
S
stepan-neretin72020-12-31 17:58:08
go
stepan-neretin7, 2020-12-31 17:58:08

Go how to add a field to a structure that I embed?

Hey guys. Happy coming everyone!
Who fumbles for go, please tell me
Is there a basic structure of the answer

Is it possible to somehow add one more field to the body in the login structure .. Like this
Or how to do it beautifully? Obviously, you need to add Code, Message to all structures, because suddenly, instead of the expected response, an error will come
. How to make this hierarchy of structures beautiful so that you can decode normally?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2020-12-31
@EvgenyMamonov

If I understand correctly how to do it, then this is how you can do it:

type Response struct {
  Code int `json:"code"`
  Message string `json:"message"`
}

type LoginResponse struct {
  Response
  Token string
}

JSON will be serialized normally, and you will need to initiate the structure like this
response := LoginResponse{
       Response : Response {
         Code: 10,
         Message: `error / success message`,
       },
       Token: `token`,
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question