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

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

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 中的对应条目。

回复列表(7|隐藏机器人聊天)
  • c
    @Ta / 2023-06-24 / /

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

  • @Ta / 2023-06-24 / /

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

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

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

  • @Ta / 2023-06-25 / /

    @ChatGPT,在golang中,访问不存在的map元素是会产生错误还是会自动创建该键值对?

  • @Ta / 2023-06-25 / /

    [GPT-3.5] [新会话] @老虎会游泳,在Go语言中,访问一个不存在的map元素不会产生错误,也不会自动创建该键值对。如果你尝试访问一个不存在的键,将会返回该map元素类型的零值。这意味着,如果map的值类型是整数,那么访问不存在的键将返回0;如果值类型是字符串,将返回空字符串;如果值类型是结构体,则返回结构体的零值等。

    如果你需要判断一个键是否存在于map中,你可以使用多返回值机制。通过使用如下形式的语法糖:

    value, ok := myMap[key]
    

    其中,value是键key对应的值,而ok是一个布尔值,表示该键是否存在于map中。如果oktrue,则键存在,并且value包含了对应的值;如果okfalse,则键不存在。

    下面是一个示例代码,演示了如何使用上述语法糖来检查map中的键是否存在:

    package main
    
    import "fmt"
    
    func main() {
        myMap := make(map[string]int)
        myMap["foo"] = 42
    
        value, ok := myMap["foo"]
        if ok {
            fmt.Println("Value:", value)
        } else {
            fmt.Println("Key not found")
        }
    
        value, ok = myMap["bar"]
        if ok {
            fmt.Println("Value:", value)
        } else {
            fmt.Println("Key not found")
        }
    }
    

    输出结果为:

    Value: 42
    Key not found
    

    在这个示例中,myMap中存在键"foo",因此第一个判断返回true,并打印出对应的值42。而对于键"bar",它不存在于myMap中,因此第二个判断返回false,并打印出"Key not found"。

  • @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}才可以被垃圾回收,在此之前它都是有效的。

添加新回复
回复需要登录