Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Find ¶
Find return a value indicated by path, path can be any of "struct field name/map index/slice index/array index/method name".
func Settable ¶
Settable return a settable value indicated by path, path can be any of "struct field name/map index/slice index/array index/method name".
Example ¶
var ts = TestStruct{ Interface: &TestStruct2{}, } var v = reflect.ValueOf(&ts) find.Settable(v, []string{"String"}).Set(reflect.ValueOf("ok")) fmt.Println(ts.String) find.Settable(v, []string{"Bool"}).Set(reflect.ValueOf(true)) fmt.Println(ts.Bool) var time = reflect.ValueOf(time.Date(2022, 1, 12, 19, 30, 30, 0, time.UTC)) find.Settable(v, []string{"Time"}).Set(time) fmt.Println(ts.Time) find.Settable(v, []string{"Interface", "Time"}).Set(time) fmt.Println(ts.Interface.(*TestStruct2).Time) find.Settable(v, []string{"Interface"}).Set(reflect.ValueOf(9)) fmt.Println(ts.Interface) var s = "string" find.Settable(v, []string{"Pointer"}).Set(reflect.ValueOf(&s)) fmt.Println(*ts.Pointer) fmt.Println(find.Settable(v, []string{"NonExists"}))
Output: ok true 2022-01-12 19:30:30 +0000 UTC 2022-01-12 19:30:30 +0000 UTC 9 string <invalid reflect.Value>
Example (Layer) ¶
var ts = TestStruct{ Layer: TestStruct2{Slice: []int{0, 1, 2}}, } var v = reflect.ValueOf(&ts).Elem() find.Settable(v, []string{"Layer", "Slice", "2"}).Set(reflect.ValueOf(22)) fmt.Println(ts.Layer.Slice[2]) fmt.Println(find.Settable(v, []string{"Layer", "Slice", "3"})) fmt.Println(find.Settable(v, []string{"Layer", "Slice", "none"})) find.Settable(v, []string{"Layer", "Map"}).Set(reflect.ValueOf(map[string][]int{"k": {7}})) fmt.Println(ts.Layer.Map) find.Settable(v, []string{"PointerLayer", "Map2"}).Set(reflect.ValueOf(map[string]int{"k": 8})) fmt.Println(ts.PointerLayer.Map2) fmt.Println(find.Settable(v, []string{"Layer", "Map", "key"})) fmt.Println(find.Settable(v, []string{"NonExists"}))
Output: 22 <invalid reflect.Value> <invalid reflect.Value> map[k:[7]] map[k:8] <invalid reflect.Value> <invalid reflect.Value>
Example (Method) ¶
var ts = TestStruct{} var v = reflect.ValueOf(&ts) find.Settable(v, []string{"SettableMethod"}).Elem().Set(reflect.ValueOf("ok")) fmt.Println(ts.String) find.Settable(v.Elem(), []string{"SettableMethod"}).Elem().Set(reflect.ValueOf("ok2")) fmt.Println(ts.String)
Output: ok ok2
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.