styp := etyp.(*StructType);
f := styp.Field(0);
testType(t, 5, f.Type, "chan *int32");
- f = styp.Field(1);
+
+ f, present := styp.FieldByName("d");
+ if !present {
+ t.Errorf("FieldByName says present field is absent");
+ }
testType(t, 6, f.Type, "float32");
+ f, present = styp.FieldByName("absent");
+ if present {
+ t.Errorf("FieldByName says absent field is present");
+ }
+
typ = Typeof(([32]int32)(nil));
testType(t, 7, typ, "[32]int32");
testType(t, 8, typ.(*ArrayType).Elem(), "int32");
Type Type;
Tag string;
Offset uintptr;
+ Index int;
Anonymous bool;
}
return;
}
+// FieldByName returns the field with the provided name and a boolean to indicate
+// that the field was found..
+func (t *StructType) FieldByName(name string) (f StructField, present bool) {
+ for i, p := range t.fields {
+ if p.name == nil || *p.name != name {
+ continue;
+ }
+ f.Name = *p.name;
+ f.Type = toType(*p.typ);
+ if p.pkgPath != nil {
+ f.PkgPath = *p.pkgPath;
+ }
+ if p.tag != nil {
+ f.Tag = *p.tag;
+ }
+ f.Offset = p.offset;
+ f.Index = i;
+ present = true;
+ break;
+ }
+ return;
+}
+
// NumField returns the number of struct fields.
func (t *StructType) NumField() int {
return len(t.fields);