Reflection/List properties: Difference between revisions
Content added Content deleted
(Add Go example.) |
|||
Line 44: | Line 44: | ||
There may be a list of items, not just the lone <code>X</code> and these proceedings apply to READ statements also. |
There may be a list of items, not just the lone <code>X</code> and these proceedings apply to READ statements also. |
||
=={{header|Go}}== |
|||
<lang Go>package main |
|||
import ( |
|||
"fmt" |
|||
"image" |
|||
"reflect" |
|||
) |
|||
// A type definition |
|||
type t struct { |
|||
X int |
|||
next *t |
|||
} |
|||
func main() { |
|||
report(t{}) |
|||
report(image.Point{}) |
|||
} |
|||
func report(x interface{}) { |
|||
t := reflect.TypeOf(x) |
|||
n := t.NumField() |
|||
fmt.Printf("Type %v has %d fields:\n", t, n) |
|||
fmt.Println("Name Type Exported") |
|||
for i := 0; i < n; i++ { |
|||
f := t.Field(i) |
|||
fmt.Printf("%-8s %-8v %-8t\n", |
|||
f.Name, |
|||
f.Type, |
|||
f.PkgPath == "", |
|||
) |
|||
} |
|||
fmt.Println() |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Type main.t has 2 fields: |
|||
Name Type Exported |
|||
X int true |
|||
next *main.t false |
|||
Type image.Point has 2 fields: |
|||
Name Type Exported |
|||
X int true |
|||
Y int true |
|||
</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |