AppendMap writes a map to the provided buffer. The writeK and writeV parameters specify the functions to use to write each key and value of the map. The returned buffer contains the encoded map. The function panics if the map is larger than math.MaxUint32 elements.
(b []byte, m map[K]V, writeK func(b []byte, k K) []byte, writeV func(b []byte, v V) []byte)
| 256 | // The returned buffer contains the encoded map. |
| 257 | // The function panics if the map is larger than math.MaxUint32 elements. |
| 258 | func AppendMap[K comparable, V any](b []byte, m map[K]V, |
| 259 | writeK func(b []byte, k K) []byte, |
| 260 | writeV func(b []byte, v V) []byte) []byte { |
| 261 | if m == nil { |
| 262 | return AppendNil(b) |
| 263 | } |
| 264 | if uint64(len(m)) > math.MaxUint32 { |
| 265 | panic(fmt.Sprintf("map too large to encode: %d elements", len(m))) |
| 266 | } |
| 267 | b = AppendMapHeader(b, uint32(len(m))) |
| 268 | for k, v := range m { |
| 269 | b = writeK(b, k) |
| 270 | b = writeV(b, v) |
| 271 | } |
| 272 | return b |
| 273 | } |
| 274 | |
| 275 | // AppendMapSorted writes a map to the provided buffer. |
| 276 | // Keys are sorted before writing. |
searching dependent graphs…