Last active
September 4, 2015 15:56
-
-
Save himulawang/fce9cdda8feb61a6522c to your computer and use it in GitHub Desktop.
Golang Reflect New
This file contains 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" | |
"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