ExamplePointer_Set_append demonstrates the RFC 6901 "-" token as an append operation on a slice. On nested slices reached through an addressable parent (map entry, pointer to struct, ...), the append is performed in place and the returned document is the same reference.
()
| 137 | // On nested slices reached through an addressable parent (map entry, pointer to struct, ...), the |
| 138 | // append is performed in place and the returned document is the same reference. |
| 139 | func ExamplePointer_Set_append() { |
| 140 | doc := map[string]any{"foo": []any{"bar"}} |
| 141 | |
| 142 | pointer, err := New("/foo/-") |
| 143 | if err != nil { |
| 144 | fmt.Println(err) |
| 145 | |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | if _, err := pointer.Set(doc, "baz"); err != nil { |
| 150 | fmt.Println(err) |
| 151 | |
| 152 | return |
| 153 | } |
| 154 | |
| 155 | fmt.Printf("doc: %v\n", doc["foo"]) |
| 156 | |
| 157 | // Output: doc: [bar baz] |
| 158 | } |
| 159 | |
| 160 | // ExamplePointer_Set_appendTopLevelSlice shows the one case where the returned document is |
| 161 | // load-bearing: appending to a top-level slice passed by value. |