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

Function ExampleReadArray_struct

msgp/iter_test.go:165–190  ·  view source on GitHub ↗

Example: slice roundtrip with struct elements using WriteArray/ReadArray. Uses testDec as the element type, with EncoderTo/DecoderFrom helpers.

()

Source from the content-addressed store, hash-verified

163// Example: slice roundtrip with struct elements using WriteArray/ReadArray.
164// Uses testDec as the element type, with EncoderTo/DecoderFrom helpers.
165func ExampleReadArray_struct() {
166 var buf bytes.Buffer
167 w := NewWriter(&buf)
168
169 in := []testDec{{A: 1, B: "x"}, {A: 2, B: "y"}}
170 // Write []testDec using EncoderTo as the per-element writer.
171 _ = WriteArray(w, in, EncoderTo(w, testDec{}))
172 _ = w.Flush()
173
174 r := NewReader(&buf)
175 // Read []testDec using DecoderFrom as the per-element reader.
176 seq := ReadArray(r, DecoderFrom(r, testDec{}))
177
178 seq(func(v testDec, err error) bool {
179 if err != nil {
180 fmt.Println("err:", err)
181 return false
182 }
183 fmt.Printf("%d %s\n", v.A, v.B)
184 return true
185 })
186
187 // Output:
188 // 1 x
189 // 2 y
190}
191
192// Example: map roundtrip with struct values using WriteMapSorted/ReadMap.
193// Uses testDec as the value type and sorts keys for deterministic output.

Callers

nothing calls this directly

Calls 7

FlushMethod · 0.95
NewWriterFunction · 0.85
WriteArrayFunction · 0.85
EncoderToFunction · 0.85
NewReaderFunction · 0.85
ReadArrayFunction · 0.85
DecoderFromFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…