Skip to content

Instantly share code, notes, and snippets.

@kmtr
Created May 4, 2014 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmtr/be13d63e15a684887216 to your computer and use it in GitHub Desktop.
Save kmtr/be13d63e15a684887216 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"reflect"
"unsafe"
)
func Put(p unsafe.Pointer, key interface{}, value interface{}) {
mapType := reflect.MapOf(reflect.TypeOf(key), reflect.TypeOf(value))
mapValue := reflect.Indirect(reflect.NewAt(mapType, p))
mapValue.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
}
func main() {
m := make(map[string]int)
m["a"] = 10
Put(unsafe.Pointer(&m), "b", 3)
fmt.Printf("%v", m)
// map[a:10 b:3]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment