| 1 |
// run |
| 2 |
|
| 3 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding" |
| 11 |
"fmt" |
| 12 |
) |
| 13 |
|
| 14 |
type Seralizable interface { |
| 15 |
encoding.BinaryMarshaler |
| 16 |
encoding.BinaryUnmarshaler |
| 17 |
} |
| 18 |
|
| 19 |
type SerDeString string |
| 20 |
|
| 21 |
func (s *SerDeString) UnmarshalBinary(in []byte) error { |
| 22 |
*s = SerDeString(in) |
| 23 |
return nil |
| 24 |
} |
| 25 |
|
| 26 |
func (s SerDeString) MarshalBinary() ([]byte, error) { |
| 27 |
return []byte(s), nil |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
type GenericSerializable[T Seralizable] struct { |
| 32 |
Key string |
| 33 |
Value T |
| 34 |
} |
| 35 |
|
| 36 |
func (g GenericSerializable[T]) Send() { |
| 37 |
out, err := g.Value.MarshalBinary() |
| 38 |
if err != nil { |
| 39 |
panic("bad") |
| 40 |
} |
| 41 |
var newval SerDeString |
| 42 |
newval.UnmarshalBinary(out) |
| 43 |
fmt.Printf("Sent %s\n", newval) |
| 44 |
} |
| 45 |
|
| 46 |
func main() { |
| 47 |
val := SerDeString("asdf") |
| 48 |
x := GenericSerializable[*SerDeString]{ |
| 49 |
Value: &val, |
| 50 |
} |
| 51 |
x.Send() |
| 52 |
} |