gpt4 book ai didi

struct - Golang - 0 不为空

转载 作者:IT王子 更新时间:2023-10-29 02:27:53 25 4
gpt4 key购买 nike

我将值传递给一个结构,其中的值具有omitempty

偏移量 uint64 'json:"offset,omitempty"'

然而,当我将 0 作为偏移值传递时,它也被省略了。

我能否以某种方式将 0 声明为未定义为 null 的值?

最佳答案

序列化结构通常使用指针来指示可为空的字段。它可以使处理结构的工作更麻烦一些,但具有区分 nil0

的优点
type T struct {
Offset *uint64 `json:"offset,omitempty"`
}

用一个空指针

t := T{}
// marshals to "{}"

在分配零值之后

t.Offset = new(uint64)
// marshals to `{"offset":0}`

关于struct - Golang - 0 不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30377861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com