如何更新Go中的地图值 [英] How to update map values in Go

查看:168
本文介绍了如何更新Go中的地图值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用字符串键和结构体值来创建一个映射,我可以通过映射键来更新映射中的结构值。



I'已经尝试了这个这个不会给我想要的输出。



我真正想要的是:

 收到的ID:D1值:V1 
收到的ID:D2值:V2
收到的ID:D3值:V3
收到的ID:D4值:V4
收到的ID:D5值:V5

数据键:D1值:更新D1的数据
数据键:D2值:UpdatedData for D2
数据键:D3值:D3的更新数据
数据键:D4值:D4的更新数据
数据键:D5值:D5的更新数据

数据键:D1值:已更新数据D1
数据键:D2值:已更新数据D2
数据键:D3值:更新数据D3
数据键:D4值:UpdatedData对于D4
数据键:D5值:D5的更新数据


解决方案

您无法更改与地图中的按键相关联的值,因此您只能重新分配值。



这会给您留下2个可能的选项:


  1. 在地图中存储指针,因此您可以修改指向的对象(不在地图数据结构中)。


  2. 存储结构值,但是当您修改它时,您需要将其重新分配给键。



  3. 1。使用指针



    在地图中存储指针: dataManaged:= map [string] * Data {}



    当你填充地图时,你不能使用循环变量,因为它在每次迭代中被覆盖。取而代之的是它的副本,并存储该副本的地址:

      for _,v:= range dataReceived {
    fmt.Println(已收到ID:,v.ID,Value:,v.Value)
    v2:= v
    dataManaged [v.ID] =& v2
    }

    输出与预期一致。试试去游乐场



    2。重新分配已修改的结构



    坚持将结构值存储在映射中: dataManaged:= map [string] Data {}



    遍历键值对将会为您提供值的副本。因此,在您修改该值后,将其重新分配给它:

      for m,n:= range dataManaged {
    n。 Value =UpdatedData for+ n.ID
    dataManaged [m] = n
    fmt.Println(Data key:,m,Value:,n.Value)
    }

    试试这个去游乐场


    I want to build a map with string key and struct value with which I'm able to update struct value in the map identified by map key.

    I've tried this and this which doesn't give me desired output.

    What I really want is this:

    Received ID: D1 Value: V1
    Received ID: D2 Value: V2
    Received ID: D3 Value: V3
    Received ID: D4 Value: V4
    Received ID: D5 Value: V5
    
    Data key: D1 Value: UpdatedData for D1
    Data key: D2 Value: UpdatedData for D2
    Data key: D3 Value: UpdatedData for D3
    Data key: D4 Value: UpdatedData for D4
    Data key: D5 Value: UpdatedData for D5
    
    Data key: D1 Value: UpdatedData for D1
    Data key: D2 Value: UpdatedData for D2
    Data key: D3 Value: UpdatedData for D3
    Data key: D4 Value: UpdatedData for D4
    Data key: D5 Value: UpdatedData for D5

    解决方案

    You can't change values associated with keys in a map, you can only reassign values.

    This leaves you 2 possible options:

    1. Store pointers in the map, so you can modify the pointed object (which is not inside the map data structure).

    2. Store struct values, but when you modify it, you need to reassign it to the key.

    1. Using pointers

    Storing pointers in the map: dataManaged := map[string]*Data{}

    When you "fill" the map, you can't use the loop's variable, as it gets overwritten in each iteration. Instead make a copy of it, and store the address of that copy:

    for _, v := range dataReceived {
        fmt.Println("Received ID:", v.ID, "Value:", v.Value)
        v2 := v
        dataManaged[v.ID] = &v2
    }
    

    Output is as expected. Try it on the Go Playground.

    2. Reassigning the modified struct

    Sticking to storing struct values in the map: dataManaged := map[string]Data{}

    Iterating over the key-value pairs will give you copies of the values. So after you modified the value, reassign it back:

    for m, n := range dataManaged {
        n.Value = "UpdatedData for " + n.ID
        dataManaged[m] = n
        fmt.Println("Data key:", m, "Value:", n.Value)
    }
    

    Try this one on the Go Playground.

    这篇关于如何更新Go中的地图值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆