初始化嵌套的匿名结构

时间:2019-11-04 15:37:49

标签: go struct

我的json为

import * as RNLocalize from "react-native-localize";

const localCode =  RNLocalize.getLocales()
const countryCode = localCode.countryCode
if (countryCode === "IN" && hours >17 || countryCode === "IN" && hours < 5) {
   themColor = "dark"
} else if (countryCode === "AU" && hours > 19 || countryCode === "AU" && hours < 5)  {
   themColor = "dark"
} else {
   themColor = "light"
}

我想将我的结构编组到json之上,并创建如下结构-

INSTALL COMMAND: 80E60C003F10A0000005591010FFFFFFFF8900000D0010A0000005591010FFFFFFFF8900000E0010A0000005591010FFFFFFFF89000020000380C00006C904810203700000

CLA: 80
INS: E6
P1: 0C
P2:00
Length of command field: 3F

*Command field:*
Length of Executable Load File AID: 10
Executable Load File: A0000005591010FFFFFFFF8900000D00

Length of Executable Module AID:10
Executable Module: A0000005591010FFFFFFFF8900000E00

Length of Application AID: 10
Application AID (ISD-P-AID): A0000005591010FFFFFFFF8900002000

Length of Privileges: 03
Privileges: 80 C0 00

Length of Install Parameters field:06
Install Parameters field: C90481020370
Length of Install Token: 00

在初始化上述结构时遇到麻烦。我不确定如何以以下方式初始化匿名结构:

{
"fields": ["time","id","status","customerId","additionalDetail"],
"pageInfo": {"start": 0, "rows": 1000}
}

我通过为页面信息创建一个单独的结构并将其与父结构附加在一起来解决此问题。但是,必须有某种方法来执行匿名嵌套结构的初始化,就像我对上面的 Fields (字符串切片)所做的一样。任何人都可以将我重定向到一些指南来做到这一点吗?

1 个答案:

答案 0 :(得分:2)

这有效,但是很丑:

bd := RBody { Fields :  []string{"time","id","status","customerId","additionalDetail"},
PageInfo : struct {Start int `json:"start"`
Rows int `json:"rows"`} {Start:1,Rows:2}}

我建议您命名匿名结构,或者在声明中初始化Fields,并在以后使用赋值初始化PageInfo。