gpt4 book ai didi

Golang JSON 标签

转载 作者:IT王子 更新时间:2023-10-29 01:08:13 25 4
gpt4 key购买 nike

假设我有一个结构Foo

Foo struct {
Bar, Baz int
}

我想像这样将这个结构编码到 json 中:{bar : 1, baz : 2}

如果不使用标签将我的单行多名称声明 (Bar, Baz int) 拆分为 2 个单独的行,我怎么能做到这一点。

这个有效:

Foo struct {
Bar int `json:"bar"`
Baz int `json:"baz"`
}

但我想:

Foo struct {
Bar, Baz int `json:???`
}

后者有可能吗?

最佳答案

根据specification , 没有。

StructType     = "struct" "{" { FieldDecl ";" } "}" .
FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] .
AnonymousField = [ "*" ] TypeName .
Tag = string_lit .

有一个严格的语法,支持一种做事的方式。

关于Golang JSON 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20339117/

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