Example: map roundtrip with struct values using WriteMapSorted/ReadMap. Uses testDec as the value type and sorts keys for deterministic output. Employs EncoderTo for values and DecoderFrom for values when reading.
()
| 193 | // Uses testDec as the value type and sorts keys for deterministic output. |
| 194 | // Employs EncoderTo for values and DecoderFrom for values when reading. |
| 195 | func ExampleReadMap_struct() { |
| 196 | var buf bytes.Buffer |
| 197 | w := NewWriter(&buf) |
| 198 | |
| 199 | in := map[string]testDec{ |
| 200 | "a": {A: 1, B: "x"}, |
| 201 | "b": {A: 2, B: "y"}, |
| 202 | } |
| 203 | // Write map[string]testDec using sorted keys for stable example output. |
| 204 | _ = WriteMapSorted(w, in, w.WriteString, EncoderTo(w, testDec{})) |
| 205 | _ = w.Flush() |
| 206 | |
| 207 | r := NewReader(&buf) |
| 208 | seq, done := ReadMap(r, r.ReadString, DecoderFrom(r, testDec{})) |
| 209 | |
| 210 | seq(func(k string, v testDec) bool { |
| 211 | fmt.Printf("%s=%d,%s\n", k, v.A, v.B) |
| 212 | return true |
| 213 | }) |
| 214 | if err := done(); err != nil { |
| 215 | fmt.Println("err:", err) |
| 216 | } |
| 217 | |
| 218 | // Output: |
| 219 | // a=1,x |
| 220 | // b=2,y |
| 221 | } |
| 222 | |
| 223 | // Example: slice roundtrip with struct elements in a byte slice using AppendArray/ReadArrayBytes. |
| 224 | // Uses testDec as the element type, with EncoderToBytes/DecoderFromBytes helpers. |
nothing calls this directly
no test coverage detected
searching dependent graphs…