Skip to content

Instantly share code, notes, and snippets.

@automaticalldramatic
Forked from ahmdrz/dump.go
Created July 19, 2019 08:54
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 automaticalldramatic/3bbfe9a9c60a378804a91999929aa7bf to your computer and use it in GitHub Desktop.
Save automaticalldramatic/3bbfe9a9c60a378804a91999929aa7bf to your computer and use it in GitHub Desktop.
Golang Reflection Example of an array.
package main
import (
"fmt"
"reflect"
)
type Test struct {
Name string
}
func main() {
result := []Test{
Test{Name: "ahmad"},
Test{Name: "reza"},
}
dump(result)
}
func dump(datasets interface{}) {
items := reflect.ValueOf(datasets)
if items.Kind() == reflect.Slice {
for i := 0; i < items.Len(); i++ {
item := items.Index(i)
if item.Kind() == reflect.Struct {
v := reflect.Indirect(item)
for j := 0; j < v.NumField(); j++ {
fmt.Println(v.Type().Field(j).Name, v.Field(j).Interface())
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment