data.go 581 B

123456789101112131415161718192021222324252627282930
  1. package render
  2. import (
  3. "net/http"
  4. "github.com/pkg/errors"
  5. )
  6. // Data common bytes struct.
  7. type Data struct {
  8. ContentType string
  9. Data [][]byte
  10. }
  11. // Render (Data) writes data with custom ContentType.
  12. func (r Data) Render(w http.ResponseWriter) (err error) {
  13. r.WriteContentType(w)
  14. for _, d := range r.Data {
  15. if _, err = w.Write(d); err != nil {
  16. err = errors.WithStack(err)
  17. return
  18. }
  19. }
  20. return
  21. }
  22. // WriteContentType writes data with custom ContentType.
  23. func (r Data) WriteContentType(w http.ResponseWriter) {
  24. writeContentType(w, []string{r.ContentType})
  25. }