Created
December 2, 2009 18:09
-
-
Save rrichardson/247402 to your computer and use it in GitHub Desktop.
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
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