gob_test.go 968 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package mhayaGOB
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. cproto "github.com/mhaya/net/proto"
  7. )
  8. func TestPB(t *testing.T) {
  9. rsp := &cproto.Response{
  10. Code: 11,
  11. Data: []byte{1, 2, 3},
  12. }
  13. gobBytes, err := Encode(rsp)
  14. if err != nil {
  15. fmt.Println(err)
  16. return
  17. }
  18. rsp1Type := reflect.TypeOf(rsp)
  19. x, err := Decode(gobBytes, []reflect.Type{rsp1Type})
  20. fmt.Println(x, err)
  21. rsp1, ok := x[0].Interface().(*cproto.Response)
  22. fmt.Println(rsp1, ok)
  23. }
  24. func TestCallFunc(t *testing.T) {
  25. type T1 struct {
  26. A int
  27. B string
  28. C int32
  29. }
  30. var (
  31. a = 1
  32. b = 2
  33. t1 = &T1{A: 1, B: "2", C: 3}
  34. )
  35. gobBytes, err := Encode(a, b, t1)
  36. if err != nil {
  37. fmt.Println(err)
  38. return
  39. }
  40. fn := func(a int, b int, c *T1) {
  41. fmt.Println("ok!!!!!!!", a, b, c)
  42. }
  43. paramsType := reflect.TypeOf(fn)
  44. paramsValue := reflect.ValueOf(fn)
  45. decodeValue, err := DecodeFunc(gobBytes, paramsType)
  46. if err != nil {
  47. fmt.Println(err)
  48. return
  49. }
  50. paramsValue.Call(decodeValue)
  51. }