struct tag 默認值 1. 需求背景有時候 gopher 在 marshal 一個 struct 到 json 的時候,想要 struct 某些屬性在沒有值的情況下有默認值,但是按照現在 marshar 的作用下不會給 struct 的屬性賦默認值,所以爲了解決這個特殊需求,我們應該按照以下方案來解決。2. 解決方案 利用反射,因爲通過反射,我們可以拿到 tag 屬性,進而對默認字段做 ⌘ Read more
struct tag 默認值 1. 需求背景有時候 gopher 在 marshal 一個 struct 到 json 的時候,想要 struct 某些屬性在沒有值的情況下有默認值,但是按照現在 marshar 的作用下不會給 struct 的屬性賦默認值,所以爲了解決這個特殊需求,我們應該按照以下方案來解決。2. 解決方案 利用反射,因爲通過反射,我們可以拿到 tag 屬性,進而對默認字段做 ⌘ Read more