for-loop - 更新 map 中的键,同时遍历该 map

标签 for-loop go

我想使用 URL 参数将 key 从一个名称更新为另一个名称。我有代码,但输出不正确。见下文。

这是 map

var data map[string][]string

调用函数的PUT方法

r.HandleFunc("/updatekey/{key}/{newkey}", handleUpdateKey).Methods("PUT")

handleUpdateKey 函数,它被记录下来并准确解释了它在做什么。

func handleUpdateKey(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)

k := params["key"] //get url params
nk := params["newkey"]

s := make([]string, len(data[k])) //create slice of string to store map variables
for i := range data {             //range over the data map
    fmt.Fprintf(w, i)
    if k != i { //check if no keys exist with URL key param
        fmt.Fprintf(w, "That KEY doesn't exist in memory")
        break //kill the loop
    } else { //if there is a key the same as the key param
        for _, values := range data[i] { //loop over the slice of string (values in that KEY)
            s = append(s, values) //append all those items to the slice of string
        }

        delete(data, k) //delete the old key

        for _, svalues := range s { //loop over the slice of string we created earlier
            data[nk] = append(data[nk], svalues) //append the items within the slice of string, to the new key... replicating the old key, with a new key name
        }
    }
}
}

下面应该将该 KEY 的所有值分配给一段字符串,我们稍后会对其进行迭代并将其添加到新的 KEY 中。这有效,但是,输出如下,这显然是不正确的

KEY: catt: VALUE: 
KEY: catt: VALUE: 
KEY: catt: VALUE: zeus
KEY: catt: VALUE: xena

旧输出:

KEY: dog: VALUE: zeus
KEY: dog: VALUE: xena

正确的新输出:

KEY: catt: VALUE: zeus
KEY: catt: VALUE: xena

最佳答案

在大多数语言中,改变你迭代的结构会导致奇怪的事情发生。特别是 map 。你必须找到另一种方法。

幸运的是,根本不需要迭代。您的循环只是一个大的 if/else 语句。如果 key 匹配,则执行某些操作。如果没有,请执行其他操作。因为是map,所以不用迭代查找key,直接查找即可。也不需要为了复制 map 值而进行所有费力的循环。

if val, ok := data[k]; ok {
    // Copy the value
    data[nk] = val
    // Delete the old key
    delete(data, k)
} else {
    fmt.Fprintf(w, "The key %v doesn't exist", k)
}

最后,避免在函数中使用全局变量。如果函数可以更改全局变量,则很难理解函数对程序的影响。 data 应该传递给函数以使其清晰。

func handleUpdateKey(w http.ResponseWriter, r *http.Request, data map[string][]string)

关于for-loop - 更新 map 中的键,同时遍历该 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54079429/

相关文章:

python - 通过迭代列来搜索列中的元素,pandas

objective-c - 枚举中的复制和自动释放

go - 将 RSA PrivateKey PEM 写入 golang 文件

go - 设置另一个进程的终端大小(ala `stty columns`)?

go - 编码(marshal)/解编码(marshal) reflect.Type

javascript - JQuery For every 循环使用 HTML 表数据

javascript - Angular js - json 上的简单循环不起作用

python - 为什么变量在循环中调用后会发生变化?

go - Go 中的 OO 风格结构对象

go - 管理通过非标准端口访问的自定义 Go 模块