Handling Type Number And String In One Struct

Tram Ho

Request:

  • With a Struct that can request data both String and Number.

Problem:

  • Initially an api adds an order with input:

  • Structure data:

  • A new requirement is to use the above api to integrate with another client, the same business requirement except that the type of amount is Number .

=> error will appear

Handle:

  • With the above requirements, an amount field with type is String, Number. Need to change type json.Number

  • With type json.Number, it is possible to convert request data both:

  • Then each type is different, just:

Full source

Thank you brother: https://t.me/OpenDevGolang join this group if you have questions.

Share the news now

Source : Viblo