GoLang Map Value : By Value or Reference ?
Map data structure in GoLang is to map keys to values.Value Retrieval
If we have a map say objects with keys as string, then we retrieve a value for any key as
objects["key"]
is Value a reference or new value?
Having come from Java background, my idea was the value retrieved from map was going to be by reference. This was not true. Value retrieved from map by above fashion actually returns new object.So if the value in the map is some struct, then any updated on the extracted value is not reflected.
Example
In this example,- we retrieve a value from a map
- We update the value
- retrieve the same value from map again
package main
import (
"fmt"
)
type Person struct {
Name string
Address string
}
func main() {
persons := map[string]Person {
"ginny" : Person {
Name : "ginny",
Address: "Hogwart",
},
}
person := persons["ginny"]
fmt.Println(person) // will print ginny and Hogwart as expected
person.Address = "Three Brooms"
fmt.Println(person) // will print ginny and Three Brooms as expected. But this is not updated into Map
person = persons["ginny"]
fmt.Println(person) // will print ginny and Hogwart not the updated values
}
What to do for reflecting the update into map?
If you want the update in value to be reflected into the map, then the mapping in map should be pointer.
persons := map[string]*Person
This is same example as above. Only we are using pointer values in the map. Here we are able to see that the update is reflected into the map.
RUN THE CODE
package main
import (
"fmt"
)
type Person struct {
Name string
Address string
}
func main() {
persons := map[string]*Person {
"ginny" : &Person {
Name : "ginny",
Address: "Hogwart",
},
}
person := persons["ginny"]
fmt.Println(*person) // will print ginny and Hogwart as expected
person.Address = "Three Brooms"
fmt.Println(*person) // will print ginny and Three Brooms as expected. But this is not updated into Map
person = persons["ginny"]
fmt.Println(*person) // will print ginny and Three Brooms as expected
}
Similar Other Cases
- Method Receiver : If a method updates the receiver, then receiver should be pointer in order to reflect the updates. RUN Example
- Slice : When objects from slices are retrieved and updated, update is not reflected in slice itself. Pointer should be used to reflect the update. RUN Example
This is how my pal Wesley Virgin's adventure launches with this SHOCKING and controversial video.
ReplyDeleteAs a matter of fact, Wesley was in the military-and soon after leaving-he unveiled hidden, "mind control" secrets that the CIA and others used to get whatever they want.
THESE are the EXACT same secrets tons of celebrities (notably those who "became famous out of nothing") and top business people used to become rich and famous.
You probably know how you use only 10% of your brain.
That's because the majority of your BRAINPOWER is UNCONSCIOUS.
Perhaps that thought has even taken place INSIDE your own head... as it did in my good friend Wesley Virgin's head 7 years ago, while riding a non-registered, beat-up trash bucket of a car without a driver's license and on his debit card.
"I'm very frustrated with going through life payroll to payroll! Why can't I become successful?"
You've been a part of those those questions, isn't it right?
Your success story is going to happen. All you need is to believe in YOURSELF.
CLICK HERE To Find Out How To Become A MILLIONAIRE