a_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "testing"
  6. "github.com/shopspring/decimal"
  7. )
  8. func Test_A(t *testing.T) {
  9. input := 2.499
  10. dec := decimal.NewFromFloat(input)
  11. fmt.Println(dec.Round(0))
  12. }
  13. func Test_B(t *testing.T) {
  14. type A struct {
  15. ProjectId int64 `json:"project_id"`
  16. Type int `json:"type"`
  17. OperationId string `json:"operation_id"`
  18. Count int `json:"count"`
  19. Timestamp int64 `json:"timestamp"`
  20. ExtraData string `json:"extra_data"`
  21. }
  22. a := A{
  23. ProjectId: 1,
  24. Count: 10,
  25. OperationId: "jfhkdfjhsdkjf",
  26. }
  27. aByte, err := jsonTest(a)
  28. if err != nil {
  29. t.Log(err)
  30. }
  31. var b A
  32. t.Log(string(aByte))
  33. err = json.Unmarshal(aByte, &b)
  34. if err != nil {
  35. t.Log(err)
  36. }
  37. t.Log(b.OperationId, "11111111")
  38. }
  39. func jsonTest(message interface{}) ([]byte, error) {
  40. messageByte, err := json.Marshal(message)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return messageByte, nil
  45. }
  46. func jsonTest(message interface{}) ([]byte, error) {
  47. messageByte, err := json.Marshal(message)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return messageByte, nil
  52. }