golang中两个map[uint64]interface会互相影响吗

@Ta 2023-06-24发布,2023-06-24修改 6771点击

import (
	"fmt"
	"testing"
)

type InterfaceConnection interface {
	DoSomething() int
}

type MyConnection struct {
	id int
}

func (c *MyConnection) DoSomething() int {
	fmt.Println("Doing something:", c.id)
	return c.id

}

func Test2(t *testing.T) {
	Connections := make(map[uint64]InterfaceConnection)
	_workerConnections := make(map[uint64]InterfaceConnection)

	conn := &MyConnection{id: 1}

	Connections[1] = conn
	_workerConnections[1] = conn
	fmt.Printf("%p", Connections[1])
	fmt.Println()
	delete(Connections, 1)
	// 此时 _workerConnections 也被修改了,对应条目被删除

	fmt.Printf("%p", _workerConnections[1])

}


//结果
//0xc0003fd550
//0xc0003fd550

gpt3.5回答1:

如果 _workerConnections 和 Connections 的 InterfaceConnection 类型的值都是通过同一个接口变量赋值得到的,那么它们引用的是同一个底层对象。在这种情况下,删除 Connections 中的条目会同时对 _workerConnections 产生影响。

因为它们引用同一个底层对象,当你删除 Connections 中的某个条目时,底层对象也会被修改,从而影响到 _workerConnections 中相应的条目。

我纠正:

不对。如果真的产生影响,那么,fmt.Printf("%p", _workerConnections[1])应该是nil, 现在他们都能输出0xc0003fd570

gpt3.5回答2:

非常抱歉,我在之前的回答中犯了个错误。我混淆了指针和接口类型的概念。当引用同一个底层对象时,删除 Connections 中的条目不会直接影响 _workerConnections。

在你的代码中,Connections 和 _workerConnections 都是 map[uint64]workerman_go.InterfaceConnection 类型的映射,但它们是两个不同的变量,分别引用独立的映射。删除 Connections 中的条目不会自动删除 _workerConnections 中的对应条目。

回复列表(5|显示机器人聊天)
  • c
    @Ta / 2023-06-24 / /

    不能互相影响啊,你这是两个map
    掌缘生灭(白)

  • @Ta / 2023-06-24 / /

    @c,map的value是同一个&Struct{}

  • c
    @Ta / 2023-06-25 / /

    @胡椒舰长,你测试过吗,按理说你这个,删除之后,会指向一个值为空的接口
    掌缘生灭(白)

  • @Ta / 2023-06-25 / /
    package main
    
    import (
    	"fmt"
    )
    
    type InterfaceConnection interface {
    	DoSomething() int
    }
    
    type MyConnection struct {
    	id int
    }
    
    func (c *MyConnection) DoSomething() int {
    	fmt.Println("Doing something:", c.id)
    	return c.id
    
    }
    
    func main() {
    	Connections := make(map[uint64]InterfaceConnection)
    	_workerConnections := make(map[uint64]InterfaceConnection)
    
    	conn := &MyConnection{id: 1}
    
    	Connections[1] = conn
    	_workerConnections[1] = conn
    
    	obj, ok := Connections[1]
    	fmt.Printf("%v, %v\n", obj, ok)
    
    	delete(Connections, 1)
    
    	obj, ok = Connections[1]
    	fmt.Printf("%v, %v\n", obj, ok)
    
    	obj, ok = _workerConnections[1]
    	fmt.Printf("%v, %v\n", obj, ok)
    }
    
    &{1}, true
    <nil>, false
    &{1}, true
    

    确实不会影响另一个

  • @Ta / 2023-06-25 / /

    map的value是同一个&Struct{}

    map的delete只是删除了键和值的对应关系,并不会对值做内存释放操作。
    除非没有地方继续引用该值,它才会被垃圾回收。
    所以直到fmt.Printf("%p", _workerConnections[1])执行完成,&MyConnection{id: 1}才可以被垃圾回收,在此之前它都是有效的。

添加新回复
回复需要登录