Documentation
¶
Index ¶
- Variables
- func GetTag(tag, key string) string
- func LookupTag(tag, key string) (value string, ok bool)
- func ParseTag(tag string) (result map[string]string)
- func Println(structs ...interface{})
- func ShallowCopy(src interface{}) interface{}
- func ShallowCopyV(src reflect.Value) reflect.Value
- func Sprint(strct interface{}) string
- func Traverse(val reflect.Value, convertNilPtr bool, ...) bool
- func TraverseType(typ reflect.Type, skip func(field reflect.StructField) bool, ...)
- func Unquote(s string) (string, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrSyntax = errors.New("invalid syntax")
Functions ¶
func LookupTag ¶
Example (False) ¶
fmt.Println(LookupTag(``, ``)) fmt.Println(LookupTag(` `, ``)) fmt.Println(LookupTag(`a`, ``)) fmt.Println(LookupTag(`a:""`, ``)) fmt.Println(LookupTag(`a:"av"`, `b`))
Output: false false false false false
Example (True) ¶
fmt.Println(LookupTag(`a:""`, `a`)) fmt.Println(LookupTag(`a:"av"`, `a`)) fmt.Println(LookupTag(`a:"av" b:"b\" v"`, `b`)) fmt.Println(LookupTag(`a:"av" b:"b v" `, `b`)) fmt.Println(LookupTag(`名称:"值 \"def"`, `名称`))
Output: true av true b" v true b v true 值 "def true
func ParseTag ¶
Example (True) ¶
fmt.Println(ParseTag(`a:""`)) fmt.Println(ParseTag(`a:"av"`)) fmt.Println(ParseTag(`a:"av" b:"b\" v"`)) fmt.Println(ParseTag(`a:"av" b:"b v" `)) fmt.Println(ParseTag(`名称:"值 \"def"`))
Output: map[a:] map[a:av] map[a:av b:b" v] map[a:av b:b v] map[名称:值 "def]
func Println ¶
func Println(structs ...interface{})
Println print a struct without zero value fields.
Example ¶
type c struct { C int } type D struct { D int } var s = struct { A int B string C c D E c }{ A: 1, B: "", C: c{C: 3}, D: D{D: 4}, } Println(s)
Output: {A:1 C:{C:3} D:{D:4}}
Example (NilStringer) ¶
t := time.Date(2019, 1, 2, 10, 11, 12, 0, time.UTC) Println(struct{ Time *time.Time }{}) Println(struct{ Time *time.Time }{&t})
Output: {} {Time:2019-01-02 10:11:12 +0000 UTC}
func ShallowCopy ¶
func ShallowCopy(src interface{}) interface{}
ShallowCopy shallow copy a struct
Example ¶
type T struct{ A, B, c int } src := T{99, 88, 97} copy := ShallowCopy(src).(T) fmt.Println(copy, &src == ©)
Output: {99 88 0} false
func ShallowCopyV ¶
ShallowCopyV shallow copy a struct value
func Traverse ¶
func Traverse( val reflect.Value, convertNilPtr bool, skip func(val reflect.Value, field reflect.StructField) bool, fn func(val reflect.Value, field reflect.StructField) bool, ) bool
Traverse traverses a reflect.Value convertNilPtr: convert anonymous nil struct pointer to non-nil or not. nil pointer to anonymous unexported struct fields are not traversed by Traverse,
Example ¶
value := getTestValue() Traverse(reflect.ValueOf(value), true, nil, func(v reflect.Value, f reflect.StructField) bool { fmt.Println(f.Name) switch v.Kind() { case reflect.Int: v.SetInt(8) case reflect.String: v.SetString(f.Name) case reflect.Bool: v.SetBool(true) return false } return true }) fmt.Println(regexp.MustCompile("0x[0-9a-f]+").ReplaceAllLiteralString( fmt.Sprintf("%+v", value), fmt.Sprintf("%+v", reflect.ValueOf(value).Elem().FieldByName("T5")), ))
Output: T1 T2 T3 T4 T5 Stop &{T1:T1 T2:{T2:T2} t3:{T3:T3} T4:8 T5:&{T5:T5} t6:<nil> Stop:true AfterStop: notExported:0}
func TraverseType ¶
func TraverseType( typ reflect.Type, skip func(field reflect.StructField) bool, fn func(field reflect.StructField), index ...int, )
TraverseType traverses a reflect.Type but pointer to anonymous unexported struct fields are always traversed by TraverseType.
Example ¶
TraverseType(reflect.TypeOf(getTestValue()), nil, func(f reflect.StructField) { fmt.Println(f.Name, f.Index) })
Output: T1 [0] T2 [1 0] T3 [2 0] T4 [3] T5 [4 0] T6 [5 0] Stop [6] AfterStop [7]
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.