Skip to content

Instantly share code, notes, and snippets.

@SuvidhaMalaviya
Last active January 28, 2023 16:53
Show Gist options
  • Save SuvidhaMalaviya/344095c37736e2f81ca27645c219df6d to your computer and use it in GitHub Desktop.
Save SuvidhaMalaviya/344095c37736e2f81ca27645c219df6d to your computer and use it in GitHub Desktop.
reflection-1-go-lang
package main
import (
"fmt"
"reflect"
)
type MyStruct struct {
Field1 int
Field2 string
Field3 *MyStruct
}
func main() {
myStruct := MyStruct{
Field1: 1,
Field2: "hello",
Field3: &MyStruct{},
}
structValue := reflect.ValueOf(myStruct)
structType := reflect.TypeOf(myStruct)
for i := 0; i < structType.NumField(); i++ {
field := structType.Field(i)
fieldValue := structValue.Field(i)
fmt.Printf("Field Name: %s, Field Type: %s\n", field.Name, fieldValue.Type())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment