dongtuo7364 2018-04-08 22:36
浏览 62

复制带有嵌套地图的结构[重复]

This question already has an answer here:

What am I trying to do?

Copy a "default" struct into a new one when needed, keeping all it's values.

Details

I am trying to copy a Chat struct:

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"`
}

I only need to do this when a new chat is introduced, and I check for membership in a map[string]Chat.

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

The full default struct is:

{
    "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":""
            }
        }
    }
}

What have I tried

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)

Do I need to change my use of map[string]Chat to map[string]*Chat and go with the second option? Pointers and references are not my specialty, so help would be appreciated.

Edit

whoever thought I was copying a map, what are you smoking?

</div>
  • 写回答

3条回答 默认 最新

  • dongyang7152 2018-04-08 22:41
    关注

    I have found in previous cases, that an easy (though not the most efficient) way to copy a complex structure is to Marshal it, and then Unmarshal it into a new variable. This can be done with a variety of encodings. Two easy ones (included in the stdlib) would be json and gob.

    There are also plenty of libraries using reflection to achieve a similar goal: https://github.com/jinzhu/copier

    But like I said, though not as efficient, this is easy to reason about and takes just a single, simple function to achieve. If performance matters you should prefer gob over json. If performance REALLY matters, then you should prefer another solution altogether.

    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失