MCPcopy
hub / github.com/tinylib/msgp / ExampleReadMap_struct

Function ExampleReadMap_struct

msgp/iter_test.go:195–221  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

193// Uses testDec as the value type and sorts keys for deterministic output.
194// Employs EncoderTo for values and DecoderFrom for values when reading.
195func 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.

Callers

nothing calls this directly

Calls 7

FlushMethod · 0.95
NewWriterFunction · 0.85
WriteMapSortedFunction · 0.85
EncoderToFunction · 0.85
NewReaderFunction · 0.85
ReadMapFunction · 0.85
DecoderFromFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…