ExampleBitSet_WriteTo serializes a BitSet to a stream and reads it back with ReadFrom. The encoding is portable across platforms.
()
| 233 | // ExampleBitSet_WriteTo serializes a BitSet to a stream and reads it back with |
| 234 | // ReadFrom. The encoding is portable across platforms. |
| 235 | func ExampleBitSet_WriteTo() { |
| 236 | b := New(100).Set(10).Set(20).Set(99) |
| 237 | |
| 238 | var buf bytes.Buffer |
| 239 | |
| 240 | _, err := b.WriteTo(&buf) |
| 241 | if err != nil { |
| 242 | panic(err) |
| 243 | } |
| 244 | |
| 245 | // ReadFrom reuses the receiver's storage where possible. |
| 246 | restored := New(0) |
| 247 | |
| 248 | _, err = restored.ReadFrom(&buf) |
| 249 | if err != nil { |
| 250 | panic(err) |
| 251 | } |
| 252 | |
| 253 | fmt.Println("equal:", restored.Equal(b)) |
| 254 | fmt.Println("bits:", restored.String()) |
| 255 | // Output: |
| 256 | // equal: true |
| 257 | // bits: {10,20,99} |
| 258 | } |
| 259 | |
| 260 | // ExampleBitSet_MarshalJSON round-trips a BitSet through JSON. BitSet |
| 261 | // implements json.Marshaler and json.Unmarshaler. |