golang结构的另一个func更改映射

user3819305:

我是新手,现在我有一个关于functoin pass变量的问题,下面是代码:

type User struct {
    Name string
    Map  map[string]string
}
func main() {
    u := User{Name: "Leto"}
    u.Map = make(map[string]string)
    fmt.Println("before --------")
    fmt.Println(unsafe.Pointer(&u))
    fmt.Println(unsafe.Pointer(&(u.Map)))
    fmt.Println(u)
    Modify(u)
    fmt.Println("after --------")
    fmt.Println(u)
}
func Modify(u User) {
    fmt.Println("in func --------")
    fmt.Println(unsafe.Pointer(&u))
    fmt.Println(unsafe.Pointer(&(u.Map)))
    u.Name = "Paul"
    u.Map["t"] = "t"
}

上面输出的代码:

before --------
0xc04203a4c0
0xc04203a4d0
{Leto map[]}
in func --------
0xc04203a500
0xc04203a510
after --------
{Leto map[t:t]}

在Modify func中,我知道用户是一个副本,因此更改名称不起作用是可以的,但是为什么将Map效果更改为用户结构?

Yadvendar:

我们需要在每个调用中了解内存分配的工作方式:

  u := User{Name: "Leto"}
// u is an object of type User
// after this line memory has been allocated to both the
// properties u.Name(string) and u.Map(reference)
// lets say allocated memory address for u.Name starts with x
// for u.Map it starts with y, and note that u.Map is a reference i.e. 
// the value contained in it will be a different memory address which
// will be the starting memory address of the actual map
// right now the value written at y is nil since it 
// does not point to any memory address
u.Map = make(map[string]string)
// now value of y has been updated to z (which is the 
// memory address of the beginning of the map initialized 
// with make call) 
fmt.Println("before --------")
fmt.Println(unsafe.Pointer(&u))
fmt.Println(unsafe.Pointer(&(u.Map)))
fmt.Println(u)
// here you are about to pass object by value 
// so basically a new object will be created of type User
// lets talk about how copy of this object will be created
// copy of u.Name will have a new address 
// lets call it x1 and the value "Leto" too will be 
// copied to memory address starting with x1
// copy of u.Map will have a new address too lets call it 
// y1 and its value z will be copied too to the memory address y1
// I think you must have got your answer by now. 
// Basically the newly copied object's property u.Map and 
// the old one's u.Map both points to the same memory address "z" 
// and hence whosoever updates the map the other one will see it
Modify(u)
fmt.Println("after --------")
fmt.Println(u)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在golang中使用相同结构中的func引用另一个字段

Golang在另一个结构的方法内更改结构的值

从另一个cpp文件更改结构内,映射键内的变量

如何在Rust中将一个结构映射到另一个结构?

Golang Map结构在另一个

Golang:另一个结构的XML属性

golang将func作为参数传递给另一个func

在Golang中将值从一个结构复制到另一个

创建一个将一个键映射到另一个键的数据结构

如何将一个结构向量映射或转换为另一个结构向量?

错误:紧急:运行时错误:无效的内存地址或nil指针取消引用。在另一个结构中存在的结构中更改映射,

Golang将结构分配给另一个结构字段不起作用

在golang中的另一个结构中重用结构

收听以调用Golang中另一个结构使用的结构函数

Golang + MongoDB嵌入式类型(在另一个结构中嵌入一个结构)

C ++更改向量内部,结构内部,另一个向量内部的结构的值

如何更改一个映射对应于另一个映射中的值的键?

另一个文件中同一包中的golang参考结构

有一个哈希映射来跟踪另一个数据结构中的元素

映射一个结构体数组,从一个 var 中过滤它们,然后提取这个结构体的另一个 var

使用属于Golang中另一个包的结构

通过与另一个数据映射来更改列名称

结构中的指针指向另一个结构

为结构分配另一个结构

扩展另一个结构内部的结构

访问另一个结构中的结构字段

在另一个结构内创建结构数组

包含对另一个结构的引用的C结构

用于定义另一个结构的typedef结构?