Created
February 21, 2019 16:13
-
-
Save xtrcode/8fdffd4a9a036517fa217046f40c59c4 to your computer and use it in GitHub Desktop.
Go Create Copy Of sync.Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func CopySyncMap(m sync.Map) sync.Map { | |
var cp sync.Map | |
m.Range(func(k, v interface{}) bool { | |
vm, ok := v.(sync.Map) | |
if ok { | |
cp.Store(k, CopySyncMap(vm)) | |
} else { | |
cp.Store(k, v) | |
} | |
return true | |
}) | |
return cp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Output:
~ go run test.go Original: 1 [5 6 7 8] 2 {{0 0} {{map[] true}} map[1337:0xc42000c028] 0} 0 [1 2 3 4] Copy: 2 {{0 0} {{map[] true}} map[1337:0xc42000c058] 0} 0 [1 2 3 4] 1 [5 6 7 8] Delete from original Original: 2 {{0 0} {{map[] true}} map[1337:0xc42000c028] 0} 1 [5 6 7 8] Copy: 0 [1 2 3 4] 1 [5 6 7 8] 2 {{0 0} {{map[] true}} map[1337:0xc42000c058] 0} Modify copy Original: 1 [5 6 7 8] 2 {{0 0} {{map[] true}} map[1337:0xc42000c028] 0} Copy: 0 [1 2 3 4] 1 [5 6 7 8] 2 {{0 0} {{map[] true}} map[] 0}