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.

52 lines
1.1 KiB

  1. package httpheader_test
  2. import (
  3. "fmt"
  4. "net/http"
  5. "github.com/mozillazg/go-httpheader"
  6. )
  7. func ExampleHeader() {
  8. type Options struct {
  9. ContentType string `header:"Content-Type"`
  10. Length int
  11. XArray []string `header:"X-Array"`
  12. TestHide string `header:"-"`
  13. IgnoreEmpty string `header:"X-Empty,omitempty"`
  14. IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
  15. CustomHeader http.Header
  16. }
  17. opt := Options{
  18. ContentType: "application/json",
  19. Length: 2,
  20. XArray: []string{"test1", "test2"},
  21. TestHide: "hide",
  22. IgnoreEmptyN: "n",
  23. CustomHeader: http.Header{
  24. "X-Test-1": []string{"233"},
  25. "X-Test-2": []string{"666"},
  26. },
  27. }
  28. h, _ := httpheader.Header(opt)
  29. fmt.Println(h["Content-Type"])
  30. fmt.Println(h["Length"])
  31. fmt.Println(h["X-Array"])
  32. _, ok := h["TestHide"]
  33. fmt.Println(ok)
  34. _, ok = h["X-Empty"]
  35. fmt.Println(ok)
  36. fmt.Println(h["X-Empty-N"])
  37. fmt.Println(h["X-Test-1"])
  38. fmt.Println(h["X-Test-2"])
  39. // Output:
  40. // [application/json]
  41. // [2]
  42. // [test1 test2]
  43. // false
  44. // false
  45. // [n]
  46. // [233]
  47. // [666]
  48. }