Created
December 17, 2016 11:40
-
-
Save hygull/0fbc428dc77bef4a665b19d598f865d8 to your computer and use it in GitHub Desktop.
Extracting structure's information using reflect package created by hygull - https://repl.it/Es0e/1
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
/* | |
{ | |
"date_of_creation" => "17 Dec 2016, Thurs", | |
"aim_of_program" => "Extracting structure's information using reflect package", | |
"coded_by" => "Rishikesh Agrawani", | |
"Go_version" => "1.7", | |
} | |
*/ | |
package main | |
import "reflect" | |
import "fmt" | |
func main() { | |
//Defining Brother structure | |
type Brother struct { | |
Name string | |
Age int | |
} | |
//Defining MySelf structure | |
type MySelf struct { | |
Name string //name string -> panic: reflect.Value.Interface: cannot return value obtained from unexported field or method | |
Age int //age int -> panic: reflect.Value.Interface: cannot return value obtained from unexported field or method | |
IsActive bool //And so on... | |
DailyExpense float32 | |
BrotherInfo Brother | |
} | |
brother := Brother{"Hemkesh", 22} | |
myself := MySelf{"Rishikesh", 24, true, 200.50, brother} | |
p1 := reflect.ValueOf(&myself).Elem() | |
fmt.Printf("%+v\n\n", myself) | |
for i := 0; i < p1.NumField(); i++ { | |
fieldName := p1.Type().Field(i).Name | |
fmt.Printf("%T , %v\n", fieldName, fieldName) | |
fieldType := p1.Field(i).Type() | |
fmt.Printf("%T, %v\n", fieldType, fieldType) | |
fieldValue := p1.Field(i).Interface() //In case of the field names that starts with smallcase, only this line will show the above errors | |
fmt.Printf("%T, %v\n\n", fieldValue, fieldValue) | |
} | |
} | |
/* OUTPUT:- | |
{Name:Rishikesh Age:24 IsActive:true DailyExpense:200.5 BrotherInfo:{Name:Hemkesh Age:22}} | |
string , Name | |
*reflect.rtype, string | |
string, Rishikesh | |
string , Age | |
*reflect.rtype, int | |
int, 24 | |
string , IsActive | |
*reflect.rtype, bool | |
bool, true | |
string , DailyExpense | |
*reflect.rtype, float32 | |
float32, 200.5 | |
string , BrotherInfo | |
*reflect.rtype, main.Brother | |
main.Brother, {Hemkesh 22} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment