Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map/Slice aliasing causes copies #10

Open
bradleyjkemp opened this issue Jan 24, 2018 · 0 comments
Open

Map/Slice aliasing causes copies #10

bradleyjkemp opened this issue Jan 24, 2018 · 0 comments

Comments

@bradleyjkemp
Copy link
Owner

The "address" of a map reflect.Value is actually the address of the struct field so although maps are reference types they get treated as if they have been copied if two e.g. structs reference the same map.
Not sure we can actually solve this

package main

import (
	"fmt"
	"reflect"
)

type container struct {
	links map[string]string
}

func main() {
	
	test := &container{
		map[string]string{"hello":"world"},
	}
	
	copied := &container{}
	copied.links = test.links
	
        // here UnsafeAddr() (which we use for unique ids) are different
	fmt.Println(reflect.ValueOf(test).Elem().Field(0).UnsafeAddr(), reflect.ValueOf(copied).Elem().Field(0).UnsafeAddr())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant