NewStruct returns a new struct with the given fields and corresponding field tags. If a field with index i has a tag, tags[i] must be that tag, but len(tags) may be only as long as required to hold the tag with the largest index i. Consequently, if no field has a tag, tags may be nil.
(fields []*Var, tags []string)
| 24 | // only as long as required to hold the tag with the largest index i. Consequently, |
| 25 | // if no field has a tag, tags may be nil. |
| 26 | func NewStruct(fields []*Var, tags []string) *Struct { |
| 27 | var fset objset |
| 28 | for _, f := range fields { |
| 29 | if f.name != "_" && fset.insert(f) != nil { |
| 30 | panic("multiple fields with the same name") |
| 31 | } |
| 32 | } |
| 33 | if len(tags) > len(fields) { |
| 34 | panic("more tags than fields") |
| 35 | } |
| 36 | s := &Struct{fields: fields, tags: tags} |
| 37 | s.markComplete() |
| 38 | return s |
| 39 | } |
| 40 | |
| 41 | // NumFields returns the number of fields in the struct (including blank and embedded fields). |
| 42 | func (s *Struct) NumFields() int { return len(s.fields) } |