| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding" |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
var ( |
| 11 |
_ encoding.BinaryMarshaler = (*customEncoding)(nil) |
| 12 |
_ encoding.BinaryUnmarshaler = (*customEncoding)(nil) |
| 13 |
) |
| 14 |
|
| 15 |
type customEncoding struct { |
| 16 |
data string |
| 17 |
} |
| 18 |
|
| 19 |
func (ce *customEncoding) MarshalBinary() ([]byte, error) { |
| 20 |
return []byte(strings.ToUpper(ce.data)), nil |
| 21 |
} |
| 22 |
|
| 23 |
func (ce *customEncoding) UnmarshalBinary(buf []byte) error { |
| 24 |
ce.data = string(buf) |
| 25 |
return nil |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func Example_customMarshaler() { |
| 30 |
hash, err := NewMap(&MapSpec{ |
| 31 |
Type: Hash, |
| 32 |
KeySize: 5, |
| 33 |
ValueSize: 4, |
| 34 |
MaxEntries: 10, |
| 35 |
}) |
| 36 |
if err != nil { |
| 37 |
panic(err) |
| 38 |
} |
| 39 |
defer hash.Close() |
| 40 |
|
| 41 |
if err := hash.Put(&customEncoding{"hello"}, uint32(111)); err != nil { |
| 42 |
panic(err) |
| 43 |
} |
| 44 |
|
| 45 |
var ( |
| 46 |
key customEncoding |
| 47 |
value uint32 |
| 48 |
entries = hash.Iterate() |
| 49 |
) |
| 50 |
|
| 51 |
for entries.Next(&key, &value) { |
| 52 |
fmt.Printf("key: %s, value: %d\n", key.data, value) |
| 53 |
} |
| 54 |
|
| 55 |
if err := entries.Err(); err != nil { |
| 56 |
panic(err) |
| 57 |
} |
| 58 |
} |