Example: writing a map using WriteMap (non-sorted). Uses a single-entry map to keep output deterministic. Use WriteMapSorted to write a sorted map.
()
| 96 | // Uses a single-entry map to keep output deterministic. |
| 97 | // Use WriteMapSorted to write a sorted map. |
| 98 | func ExampleWriteMap() { |
| 99 | var buf bytes.Buffer |
| 100 | w := NewWriter(&buf) |
| 101 | |
| 102 | // Write a map {"only":1} using WriteMap |
| 103 | _ = WriteMap(w, map[string]int{"only": 1}, w.WriteString, w.WriteInt) |
| 104 | _ = w.Flush() |
| 105 | |
| 106 | r := NewReader(&buf) |
| 107 | seq, done := ReadMap(r, r.ReadString, r.ReadInt) |
| 108 | seq(func(k string, v int) bool { |
| 109 | fmt.Printf("%s=%d\n", k, v) |
| 110 | return true |
| 111 | }) |
| 112 | if err := done(); err != nil { |
| 113 | fmt.Println("err:", err) |
| 114 | } |
| 115 | |
| 116 | // Output: |
| 117 | // only=1 |
| 118 | } |
| 119 | |
| 120 | // Example: reading an array of strings from a byte slice using ReadArrayBytes. |
| 121 | // It prints each element and then checks for a final error from the returned closure. |