example_test.go 479 B

123456789101112131415161718192021222324252627
  1. package jsoniter_test
  2. import (
  3. "fmt"
  4. "github.com/json-iterator/go"
  5. "os"
  6. )
  7. func ExampleMarshal() {
  8. type ColorGroup struct {
  9. ID int
  10. Name string
  11. Colors []string
  12. }
  13. group := ColorGroup{
  14. ID: 1,
  15. Name: "Reds",
  16. Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
  17. }
  18. b, err := jsoniter.Marshal(group)
  19. if err != nil {
  20. fmt.Println("error:", err)
  21. }
  22. os.Stdout.Write(b)
  23. // Output:
  24. // {"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}
  25. }