Skip to content

Instantly share code, notes, and snippets.

@himulawang
Last active September 4, 2015 15:56
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 himulawang/fce9cdda8feb61a6522c to your computer and use it in GitHub Desktop.
Save himulawang/fce9cdda8feb61a6522c to your computer and use it in GitHub Desktop.
Golang Reflect New
package main
import (
"fmt"
"reflect"
)
func main() {
// one way is to have a value of the type you want already
a := 1
// reflect.New works kind of like the built-in function new
// We'll get a reflected pointer to a new int value
intPtr := reflect.New(reflect.TypeOf(a))
// Just to prove it
b := intPtr.Elem().Interface().(int)
// Prints 0
fmt.Println(b)
// We can also use reflect.New without having a value of the type
var nilInt *int
intType := reflect.TypeOf(nilInt).Elem()
intPtr2 := reflect.New(intType)
// Same as above
c := intPtr2.Elem().Interface().(int)
// Prints 0 again
fmt.Println(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment