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.

63 lines
1.7 KiB

  1. # go-httpheader
  2. go-httpheader is a Go library for encoding structs into Header fields.
  3. [![Build Status](https://img.shields.io/travis/mozillazg/go-httpheader/master.svg)](https://travis-ci.org/mozillazg/go-httpheader)
  4. [![Coverage Status](https://img.shields.io/coveralls/mozillazg/go-httpheader/master.svg)](https://coveralls.io/r/mozillazg/go-httpheader?branch=master)
  5. [![Go Report Card](https://goreportcard.com/badge/github.com/mozillazg/go-httpheader)](https://goreportcard.com/report/github.com/mozillazg/go-httpheader)
  6. [![GoDoc](https://godoc.org/github.com/mozillazg/go-httpheader?status.svg)](https://godoc.org/github.com/mozillazg/go-httpheader)
  7. ## install
  8. `go get -u github.com/mozillazg/go-httpheader`
  9. ## usage
  10. ```go
  11. package main
  12. import (
  13. "fmt"
  14. "net/http"
  15. "github.com/mozillazg/go-httpheader"
  16. )
  17. type Options struct {
  18. hide string
  19. ContentType string `header:"Content-Type"`
  20. Length int
  21. XArray []string `header:"X-Array"`
  22. TestHide string `header:"-"`
  23. IgnoreEmpty string `header:"X-Empty,omitempty"`
  24. IgnoreEmptyN string `header:"X-Empty-N,omitempty"`
  25. CustomHeader http.Header
  26. }
  27. func main() {
  28. opt := Options{
  29. hide: "hide",
  30. ContentType: "application/json",
  31. Length: 2,
  32. XArray: []string{"test1", "test2"},
  33. TestHide: "hide",
  34. IgnoreEmptyN: "n",
  35. CustomHeader: http.Header{
  36. "X-Test-1": []string{"233"},
  37. "X-Test-2": []string{"666"},
  38. },
  39. }
  40. h, _ := httpheader.Header(opt)
  41. fmt.Printf("%#v", h)
  42. // h:
  43. // http.Header{
  44. // "X-Test-1": []string{"233"},
  45. // "X-Test-2": []string{"666"},
  46. // "Content-Type": []string{"application/json"},
  47. // "Length": []string{"2"},
  48. // "X-Array": []string{"test1", "test2"},
  49. // "X-Empty-N": []string{"n"},
  50. //}
  51. }
  52. ```