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

Function ExampleReadMapBytes_struct

msgp/iter_test.go:251–275  ·  view source on GitHub ↗

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.

()

Source from the content-addressed store, hash-verified

249// Uses testDec as the value type and sorts keys for deterministic output.
250// Employs EncoderToBytes for values and DecoderFromBytes for values when reading.
251func 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.

Callers

nothing calls this directly

Calls 4

AppendMapSortedFunction · 0.85
EncoderToBytesFunction · 0.85
ReadMapBytesFunction · 0.85
DecoderFromBytesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…