codekingpro/portable-devtools
114k
1// run2 3// Copyright 2021 The Go Authors. All rights reserved.4// Use of this source code is governed by a BSD-style5// license that can be found in the LICENSE file.6 7package main8 9import (10 "encoding"11 "fmt"12)13 14type Seralizable interface {15 encoding.BinaryMarshaler16 encoding.BinaryUnmarshaler17}18 19type SerDeString string20 21func (s *SerDeString) UnmarshalBinary(in []byte) error {22 *s = SerDeString(in)23 return nil24}25 26func (s SerDeString) MarshalBinary() ([]byte, error) {27 return []byte(s), nil28}29 30 31type GenericSerializable[T Seralizable] struct {32 Key string33 Value T34}35 36func (g GenericSerializable[T]) Send() {37 out, err := g.Value.MarshalBinary()38 if err != nil {39 panic("bad")40 }41 var newval SerDeString42 newval.UnmarshalBinary(out)43 fmt.Printf("Sent %s\n", newval)44}45 46func main() {47 val := SerDeString("asdf")48 x := GenericSerializable[*SerDeString]{49 Value: &val,50 }51 x.Send()52}53 