Skip to content

Instantly share code, notes, and snippets.

@jonbodner
Created December 12, 2017 18:04
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 jonbodner/bf8a25ffdca371755ddce7c01983c8f5 to your computer and use it in GitHub Desktop.
Save jonbodner/bf8a25ffdca371755ddce7c01983c8f5 to your computer and use it in GitHub Desktop.
func main() {
// declaring these vars, so I can make a reflect.Type
intSlice := make([]int, 0)
mapStringInt := make(map[string]int)
// here are the reflect.Types
sliceType := reflect.TypeOf(intSlice)
mapType := reflect.TypeOf(mapStringInt)
// and here are the new values that we are making
intSliceReflect := reflect.MakeSlice(sliceType, 0, 0)
mapReflect := reflect.MakeMap(mapType)
// and here we are using them
v := 10
rv := reflect.ValueOf(v)
intSliceReflect = reflect.Append(intSliceReflect, rv)
intSlice2 := intSliceReflect.Interface().([]int)
fmt.Println(intSlice2)
k := "hello"
rk := reflect.ValueOf(k)
mapReflect.SetMapIndex(rk, rv)
mapStringInt2 := mapReflect.Interface().(map[string]int)
fmt.Println(mapStringInt2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment