text.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package render
  5. import (
  6. "fmt"
  7. "io"
  8. "net/http"
  9. )
  10. // String contains the given interface object slice and its format.
  11. type String struct {
  12. Format string
  13. Data []interface{}
  14. }
  15. var plainContentType = []string{"text/plain; charset=utf-8"}
  16. // Render (String) writes data with custom ContentType.
  17. func (r String) Render(w http.ResponseWriter) error {
  18. return WriteString(w, r.Format, r.Data)
  19. }
  20. // WriteContentType (String) writes Plain ContentType.
  21. func (r String) WriteContentType(w http.ResponseWriter) {
  22. writeContentType(w, plainContentType)
  23. }
  24. // WriteString writes data according to its format and write custom ContentType.
  25. func WriteString(w http.ResponseWriter, format string, data []interface{}) (err error) {
  26. writeContentType(w, plainContentType)
  27. if len(data) > 0 {
  28. _, err = fmt.Fprintf(w, format, data...)
  29. return
  30. }
  31. _, err = io.WriteString(w, format)
  32. return
  33. }