I'm currently learning golang, and have the following code. The idea is to have an object with a number of pointers to it, and I'd like to modify and delete the object using one of the pointers.
package main
import "fmt"
type obj struct {
a int
b string
}
func main() {
o1 := &obj{1, "hello"}
o2 := &obj{2, "world"}
m := map[string]*obj{
"foo": o1,
"bar": o2,
}
fmt.Printf("1: %t %v
", o1, o1)
fmt.Println("2:", m, m["foo"], o1)
o1.b = "WWWW"
fmt.Println("3:", m, m["foo"], o1)
o1 = nil
fmt.Println("4:", m, m["foo"], o1)
}
http://play.golang.org/p/lqQviVuTQN
Output:
1: &{%!t(int=1) %!t(string=hello)} &{1 hello}
2: map[foo:0x10434120 bar:0x10434130] &{1 hello} &{1 hello}
3: map[foo:0x10434120 bar:0x10434130] &{1 WWWW} &{1 WWWW}
4: map[foo:0x10434120 bar:0x10434130] &{1 WWWW} <nil>
Changing object's internals works as I expect (#3).
However when I try deleting the actual object (#4) it seems just nil
s the pointer itself without touching actual object.
What am I missing?