Skip to content

Instantly share code, notes, and snippets.

@rrichardson
Created December 2, 2009 18:09
Show Gist options
  • Save rrichardson/247402 to your computer and use it in GitHub Desktop.
Save rrichardson/247402 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "reflect"
type Cloneable interface {
Clone() Cloneable;
}
type Mapable interface {
MapableDummy();
}
type MapA map[int]string;
type MapB map[string]int;
func (m MapA) MapableDummy() {;}
func (m MapB) MapableDummy() {;}
func (m Mapable) Clone() Cloneable {
omap := reflect.MakeMap(reflect.Typeof(m).(*reflect.MapType));
nmap := reflect.NewValue(reflect.Typeof(m).(*reflect.MapType));
keys := omap.Keys();
for k := range(keys) {
nmap.SetElem(k, omap.Elem(k));
}
return nmap.(Cloneable);
}
func main() {
m1 := make(MapA);
m1[1] = "foo";
//var m2 MapB;
c := m1.Clone();
fmt.Println(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment