gpt4 book ai didi

pointers - 使用嵌套映射复制结构

转载 作者:数据小太阳 更新时间:2023-10-29 03:03:21 24 4
gpt4 key购买 nike

<分区>

我想做什么?

在需要时将“默认”结构复制到一个新结构中,并保留其所有值。

详情

我正在尝试复制一个 Chat 结构:

type ChatData struct {
User map[string]map[string]string `json:"user"`
Chat map[string]string `json:"chat"`
}
type Chat struct {
Settings map[string]map[string]interface{} `json:"settings"`
Data ChatData `json:"data"`
}

我只需要在引入新聊天时执行此操作,并检查 map[string]Chat 中的成员资格。

//data is a map[string]Chat
if _, exists := data[event.Chat]; !exists {
data[event.Chat] = data["default"]
}

完整的默认结构是:

{
"default": {
"settings": {
"eightball": {
"messages": [
"yes",
"no",
"maybe"
]
},
"main": {
"blacklistedCommands": [],
"blacklistedUsers": [],
"error": "there was an error - ",
"maxConsecutive": 5,
"permissions": "You don't have permissions for that command.",
"success": "The command was successful.",
"whitelistedCommands": [],
"whitelistedUsers": []
}
},
"data": {
"user": {
"default": {
"admin": "false",
"consecutiveCommands": "0",
"nickname": "",
"sentMessages": "0"
},
"total": {
"admin": "false",
"consecutiveCommands": "0",
"nickname": "",
"sentMessages": "0"
}
},
"chat": {
"commandSender": "",
"lastMessage": "",
"lastSender": "",
"lastTimestamp": "",
"wasCommand":""
}
}
}
}

我尝试了什么

data[event.Chat] = data["default"]
// causes a reference

data[event.Chat] = &data["default"]
// cannot use &data["default"] (type *Chat) as type Chat in assignment

data[event.Chat] = *data["default"]
// invalid indirect of data["default"] (type Chat)

我是否需要将我对 map[string]Chat 的使用更改为 map[string]*Chat 并使用第二个选项?指针和引用不是我的专长,因此不胜感激。

编辑

谁以为我在抄 map ,你在抽什么烟?

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