执行:通过其指针删除对象

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 nils the pointer itself without touching actual object.

What am I missing?

All assignments in Go are copy by value.

 m := map[string]*obj{
        "foo": o1,
        "bar": o2,
    }

is an assignment, so value of foo is a copy of o1. To achieve your goal you need one more level of indirection

o1 := &obj{1, "hello"}
o2 := &obj{2, "world"}

    m := map[string]**obj{
        "foo": &o1,
        "bar": &o2,
    }

http://play.golang.org/p/XutneOziaM

Explaining @Uvelichitel's note on copy by value,

o1 := <0x10434120>
m := map[string]*obj{
    "foo": <0x10434120>,
}

o1.a = "WWW" // <0x10434120>.a = "WWW" changing both places

o1 = nil
m["foo"] // still is <0x10434120>