You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.2 KiB

7 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. wire "github.com/tendermint/go-wire"
  6. )
  7. func main() {
  8. encode(uint8(6))
  9. encode(uint32(6))
  10. encode(int8(-6))
  11. encode(int32(-6))
  12. Break()
  13. encode(uint(6))
  14. encode(uint(70000))
  15. encode(int(-6))
  16. encode(int(-70000))
  17. Break()
  18. encode("a")
  19. encode("hello")
  20. encode("¥")
  21. Break()
  22. encode([4]int8{1, 2, 3, 4})
  23. encode([4]int16{1, 2, 3, 4})
  24. encode([4]int{1, 2, 3, 4})
  25. encode([2]string{"abc", "efg"})
  26. Break()
  27. encode([]int8{1, 2, 3, 4})
  28. encode([]int16{1, 2, 3, 4})
  29. encode([]int{1, 2, 3, 4})
  30. encode([]string{"abc", "efg"})
  31. Break()
  32. timeFmt := "Mon Jan 2 15:04:05 -0700 MST 2006"
  33. t1, _ := time.Parse(timeFmt, timeFmt)
  34. n := (t1.UnixNano() / 1000000.) * 1000000
  35. encode(n)
  36. encode(t1)
  37. t2, _ := time.Parse(timeFmt, "Thu Jan 1 00:00:00 -0000 UTC 1970")
  38. encode(t2)
  39. t2, _ = time.Parse(timeFmt, "Thu Jan 1 00:00:01 -0000 UTC 1970")
  40. fmt.Println("N", t2.UnixNano())
  41. encode(t2)
  42. Break()
  43. encode(struct {
  44. A int
  45. B string
  46. C time.Time
  47. }{
  48. 4,
  49. "hello",
  50. t1,
  51. })
  52. }
  53. func encode(i interface{}) {
  54. Println(wire.BinaryBytes(i))
  55. }
  56. func Println(b []byte) {
  57. s := "["
  58. for _, x := range b {
  59. s += fmt.Sprintf("0x%.2X, ", x)
  60. }
  61. s = s[:len(s)-2] + "]"
  62. fmt.Println(s)
  63. }
  64. func Break() {
  65. fmt.Println("------")
  66. }