Go-recursive is Golang library to walk through and update nested objects. It goes down structs, maps, slices recursively.
Incrememt Int value inside nested struct:
-
Define function:
func incrementFunc(obj interface{}, level int) interface{} { if i, ok := obj.(int); ok { i += +1 return i } return obj // return unchanged }
-
Define object. Let it be complex object here - nested structs inside map:
type NestedStruct struct { Int int Nested *NestedStruct } var( n = NestedStruct{Int: 20} obj = map[int]NestedStruct{ 1: NestedStruct{Int: 10, Nested: &n}, 2: NestedStruct{}, } )
-
Execute:
recursive.Go(&obj, incrementFunc)
-
Check result:
Before: map[1:{Int:10 Nested:0xc000010230} 2:{Int:0 Nested:<nil>}] (map) >>>> {Int:0 Nested:<nil>} (struct) >>>>>>>> 0 (int) >>>>>>>> <nil> (ptr) >>>> {Int:10 Nested:0xc000010230} (struct) >>>>>>>> 10 (int) >>>>>>>> &{Int:20 Nested:<nil>} (ptr) >>>>>>>>>>>> {Int:20 Nested:<nil>} (struct) >>>>>>>>>>>>>>>> 20 (int) >>>>>>>>>>>>>>>> <nil> (ptr) After: map[1:{Int:11 Nested:0xc000010230} 2:{Int:1 Nested:<nil>}] (map) >>>> {Int:1 Nested:<nil>} (struct) >>>>>>>> 1 (int) >>>>>>>> <nil> (ptr) >>>> {Int:11 Nested:0xc000010230} (struct) >>>>>>>> 11 (int) >>>>>>>> &{Int:21 Nested:<nil>} (ptr) >>>>>>>>>>>> {Int:21 Nested:<nil>} (struct) >>>>>>>>>>>>>>>> 21 (int) >>>>>>>>>>>>>>>> <nil> (ptr)
Check examples directory for complete program.