How do you marshal a sql.NullString such that the output is flattened to give just the value in go?

后端 未结 3 2028
没有蜡笔的小新
没有蜡笔的小新 2021-02-15 05:18

Given a go struct

type Company struct {
    ID   int             `json:\"id\"`              
    Abn  sql.NullString  `json:\"abn,string\"`
}

w

3条回答
  •  清歌不尽
    2021-02-15 05:50

    The question suggests that you want to expose your database structure as JSON (presumably REST-ish) API. Unless the project is going to have a short lifespan or the logic layer is trivial, such an approach is considered an antipattern. The internals (database structure) become coupled with the external interface (API) and may result in a high cost of making a change.

    I'm attaching some reads as Google is full of tutorials on how to do the opposite:

    https://lostechies.com/jimmybogard/2016/05/12/entities-arent-resources-resources-arent-representations/

    https://thorben-janssen.com/dont-expose-entities-in-api/

提交回复
热议问题