本文整理汇总了Golang中reflect.StructType.NumField方法的典型用法代码示例。如果您正苦于以下问题:Golang StructType.NumField方法的具体用法?Golang StructType.NumField怎么用?Golang StructType.NumField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.StructType
的用法示例。
在下文中一共展示了StructType.NumField方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: enc_struct
func enc_struct(val *reflect.StructValue, typ *reflect.StructType, buffer *bytes.Buffer) os.Error {
buffer.WriteString("{")
first := true
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
if f.Tag == "" {
continue
}
tags := strings.Split(f.Tag[3:len(f.Tag)-1], ",", -1)
l := buffer.Len()
if first {
buffer.WriteString(fmt.Sprintf("\"%v\":", tags[1]))
} else {
buffer.WriteString(fmt.Sprintf(",\"%v\":", tags[1]))
}
written, err := enc(val.Field(i), f.Type, buffer, tags[2] == "req")
if err != nil {
return err
}
if !written {
buffer.Truncate(l)
} else {
first = false
}
}
buffer.WriteString("}")
return nil
}
示例2: compileStruct
func compileStruct(t *reflect.StructType) map[string]reflect.StructField {
m := make(map[string]reflect.StructField)
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.PkgPath != "" {
// ignore if not exported
continue
}
name := f.Name
if f.Tag != "" {
name = f.Tag
}
m[name] = f
}
return m
}
示例3: dec_struct
func dec_struct(val *reflect.StructValue, typ *reflect.StructType, json map[string]interface{}) os.Error {
for i := 0; i < typ.NumField(); i++ {
f := typ.Field(i)
if f.Tag == "" {
continue
}
tags := strings.Split(f.Tag[3:len(f.Tag)-1], ",", -1)
j, ok := json[tags[1]]
if !ok {
if tags[2] == "req" {
return os.NewError("Field " + f.Name + " is missing")
}
continue
}
err := dec(val.Field(i), f.Type, j)
if err != nil {
return err
}
}
return nil
}
示例4: attributes
func attributes(m interface{}) (map[string]reflect.Type, reflect.Type) {
var st *reflect.StructType
var typ reflect.Type
if _, ok := reflect.Typeof(m).(*reflect.PtrType); ok {
typ = reflect.Typeof(m).(*reflect.PtrType).Elem()
} else {
typ = reflect.Typeof(m)
}
st = typ.(*reflect.StructType)
//fmt.Println(st.NumField())
ret := make(map[string]reflect.Type)
for i := 0; i < st.NumField(); i++ {
p := st.Field(i)
//fmt.Println(p.Name)
if !p.Anonymous {
ret[p.Name] = p.Type
}
}
return ret, typ
}
示例5: unmarshal
//.........这里部分代码省略.........
if f, ok := typ.FieldByName("XMLName"); ok {
// Validate element name.
if f.Tag != "" {
tag := f.Tag
ns := ""
i := strings.LastIndex(tag, " ")
if i >= 0 {
ns, tag = tag[0:i], tag[i+1:]
}
if tag != start.Name.Local {
return UnmarshalError("expected element type <" + tag + "> but have <" + start.Name.Local + ">")
}
if ns != "" && ns != start.Name.Space {
e := "expected element <" + tag + "> in name space " + ns + " but have "
if start.Name.Space == "" {
e += "no name space"
} else {
e += start.Name.Space
}
return UnmarshalError(e)
}
}
// Save
v := sv.FieldByIndex(f.Index)
if _, ok := v.Interface().(Name); !ok {
return UnmarshalError(sv.Type().String() + " field XMLName does not have type xml.Name")
}
v.(*reflect.StructValue).Set(reflect.NewValue(start.Name).(*reflect.StructValue))
}
// Assign attributes.
// Also, determine whether we need to save character data or comments.
for i, n := 0, typ.NumField(); i < n; i++ {
f := typ.Field(i)
switch f.Tag {
case "attr":
strv, ok := sv.FieldByIndex(f.Index).(*reflect.StringValue)
if !ok {
return UnmarshalError(sv.Type().String() + " field " + f.Name + " has attr tag but is not type string")
}
// Look for attribute.
val := ""
k := strings.ToLower(f.Name)
for _, a := range start.Attr {
if fieldName(a.Name.Local) == k {
val = a.Value
break
}
}
strv.Set(val)
case "comment":
if saveComment == nil {
saveComment = sv.FieldByIndex(f.Index)
}
case "chardata":
if saveData == nil {
saveData = sv.FieldByIndex(f.Index)
}
}
}
}
// Find end element.