github.com/searKing/golang/go@v1.2.117/reflect/field.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package reflect 6 7 import ( 8 "reflect" 9 ) 10 11 // A field represents a single field found in a struct. 12 type field struct { 13 sf reflect.StructField 14 15 name string 16 nameBytes []byte // []byte(name) 17 equalFold func(s, t []byte) bool // bytes.EqualFold or equivalent 18 19 tag bool 20 index []int 21 typ reflect.Type 22 omitEmpty bool 23 quoted bool 24 } 25 26 // v[i][j][...], v[i] is v's ith field, v[i][j] is v[i]'s jth field 27 func ValueByStructFieldIndex(v reflect.Value, index []int) reflect.Value { 28 for _, i := range index { 29 if v.Kind() == reflect.Ptr { 30 if v.IsNil() { 31 return reflect.Value{} 32 } 33 v = v.Elem() 34 } 35 v = v.Field(i) 36 } 37 return v 38 } 39 40 // t[i][j][...], t[i] is t's ith field, t[i][j] is t[i]'s jth field 41 func TypeByStructFieldIndex(t reflect.Type, index []int) reflect.Type { 42 for _, i := range index { 43 if t.Kind() == reflect.Ptr { 44 t = t.Elem() 45 } 46 t = t.Field(i).Type 47 } 48 return t 49 } 50 51 func IsFieldExported(sf reflect.StructField) bool { 52 return sf.PkgPath == "" 53 }