变量镜像其他的

时间:2018-04-06 22:58:12

标签: go

所以我创建了一个简单的聊天机器人,并且我在使用其中一个命令/resetall时遇到了问题。它应该将每个用户值更改为用户的值"默认"。但是,似乎默认值导致所有值更改为2.

"data": {
    "user": {
        "bob": {
            "admin": "true",
            "consecutiveCommands": "0",
            "nickname": "", 
            "sentMessages": "2" 
        },  
        "default": {
            "admin": "true",
            "consecutiveCommands": "0",
            "nickname": "", 
            "sentMessages": "2" 
        },  
        "me": {
            "admin": "true",
            "consecutiveCommands": "0",
            "nickname": "", 
            "sentMessages": "2" 
        },  
        "total": {
            "admin": "true",
            "consecutiveCommands": "0",
            "nickname": "", 
            "sentMessages": "2" 
        }   
    },  
    "chat": {
        "commandSender": "me",
        "lastImage": "", 
        "lastMessage": "/pong",
        "lastSender": "me",
        "lastTimestamp": "11:59",
        "wasCommand": "true"
    }
}

和我的代码:

// each incoming message
type Message struct {
        Message   string
        From      string
        Chat      string
        Timestamp string
        IsCommand bool
}
//adds one to a string
func addOne(s string) string {
        i, _ := strconv.Atoi(s)
        return strconv.Itoa(i + 1)
}
//counts messages sent
func messageCounter(data map[string]Chat, event *Message) map[string]Chat {
        //counts messages sent by user
        data[event.Chat].Data.User[event.From]["sentMessages"] = addOne(data[event.Chat].Data.User[event.From]["sentMessages"])
        data[event.Chat].Data.User["total"]["sentMessages"] = addOne(data[event.Chat].Data.User["total"]["sentMessages"])
        return data
}
//sets variables for future use / other functions
func eventRecorder(data map[string]Chat, event *Message) map[string]Chat {
        if !event.IsCommand {
                data[event.Chat].Data.Chat["lastMessage"] = event.Message
                data[event.Chat].Data.Chat["lastSender"] = event.From
                data[event.Chat].Data.Chat["lastTimestamp"] = event.Timestamp
                data[event.Chat].Data.Chat["wasCommand"] = "false"
        } else {
                data[event.Chat].Data.Chat["wasCommand"] = "true"
                data[event.Chat].Data.Chat["commandSender"] = event.From
        }
        return data
}
//supposed to set all users data to the default user
func resetall(event *Message, data map[string]Chat) (error, map[string]Chat) {
        default_user := data[event.Chat].Data.User["default"]
        if data[event.Chat].Data.User[event.From]["admin"] == "true" {
                for user, _ := range data[event.Chat].Data.User {
                        if user != "default" {
                                data[event.Chat].Data.User[user] = default_user
                                print(user + "\n")
                        }   
                }   

                return nil, data
        }   
        return errors.New("don't have permission")), data
}
func main() {
        processingFuncs := []func(map[string]Chat, *Message) map[string]Chat{
                messageCounter,
                eventRecorder,
        }   
        data, _ := readsettings() //reads the data from a json file
        event := &Message{"/resetall", "me", "chat123", "11:59", false}
        if strings.Split(event.Message, " ")[0] == "/resetall" {
                event.IsCommand = true
                _, data = resetall(event, data)
                fmt.Println("success")
        }   
        for _, processingFunc := range processingFuncs {
                processingFunc(data, event)
        }   
        writesettings(data) //writes the data to a json file
}

因此,如果我将每个人的消息计数器设置为0并运行它,它会将每个用户的消息计数器设置为2(包括默认值)。每次我运行它,值增加2.任何人都可以帮助解释为什么会发生这种情况

1 个答案:

答案 0 :(得分:0)

此行复制用户

data[event.Chat].Data.User[user] = default_user

对它做了参考,我不知道为什么我之前没有发现它。我用这段代码替换了它

for k, v := range data[event.Chat].Data.User["default"] {
    data[event.Chat].Data.User[user][k] = v 
} 

它完美无缺。