Example: map roundtrip with struct values in a byte slice using AppendMapSorted/ReadMapBytes. Uses testDec as the value type and sorts keys for deterministic output. Employs EncoderToBytes for values and DecoderFromBytes for values when reading.
()
| 249 | // Uses testDec as the value type and sorts keys for deterministic output. |
| 250 | // Employs EncoderToBytes for values and DecoderFromBytes for values when reading. |
| 251 | func ExampleReadMapBytes_struct() { |
| 252 | in := map[string]testDec{ |
| 253 | "a": {A: 1, B: "x"}, |
| 254 | "b": {A: 2, B: "y"}, |
| 255 | } |
| 256 | var b []byte |
| 257 | |
| 258 | // Append map[string]testDec with sorted keys for stable example output. |
| 259 | b = AppendMapSorted(b, in, AppendString, EncoderToBytes(testDec{})) |
| 260 | |
| 261 | // Read back using DecoderFromBytes for values. |
| 262 | seq, finish := ReadMapBytes(b, ReadStringBytes, DecoderFromBytes(testDec{})) |
| 263 | |
| 264 | seq(func(k string, v testDec) bool { |
| 265 | fmt.Printf("%s=%d,%s\n", k, v.A, v.B) |
| 266 | return true |
| 267 | }) |
| 268 | if _, err := finish(); err != nil { |
| 269 | fmt.Println("err:", err) |
| 270 | } |
| 271 | |
| 272 | // Output: |
| 273 | // a=1,x |
| 274 | // b=2,y |
| 275 | } |
| 276 | |
| 277 | // Example: appending a map to a byte slice using AppendMap (non-sorted). |
| 278 | // Uses a single-entry map to keep output deterministic. |
nothing calls this directly
no test coverage detected
searching dependent graphs…