go - Can't create copy of variable of type map[int]Struct in another variable by just assigning one variable to another -
i have map :
cart := map[10033207:{10033207 3 425 126} 10012761:{10012761 4 40 0}]
i want create copy of cart
in variable tempcart
can modify tempcart temporary usage in function. want cart value remains same.
tempcart := cart //some operation modifies temp cart , make //map[10033207:{10033207 2 425 126} 10012761:{10012761 1 40 0}]
the problem when modify tempcart
, somehow cart
getting modified , becomes equal
tempcart
.
later when print value of cart
get: map[10033207:{10033207 2 425 126} 10012761:{10012761 1 40 0}]
, not original value map[10033207:{10033207 3 425 126} 10012761:{10012761 4 40 0}]
.
i can't understand reason behind , want know solution of how create copy of cart
.
edit: question has been marked duplicate copy 1 map another
knew how copy 1 map anothor, prime question why couldn't assign 1 map variable. why have copy in loop.
to copy map use
for k,v := range originalmap { newmap[k] = v }
Comments
Post a Comment