text.go 633 B

12345678910111213141516171819202122232425262728293031
  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. type String struct {
  11. Format string
  12. Data []interface{}
  13. }
  14. var plainContentType = []string{"text/plain; charset=utf-8"}
  15. func (r String) Render(w http.ResponseWriter) error {
  16. header := w.Header()
  17. if _, exist := header["Content-Type"]; !exist {
  18. header["Content-Type"] = plainContentType
  19. }
  20. if len(r.Data) > 0 {
  21. fmt.Fprintf(w, r.Format, r.Data...)
  22. } else {
  23. io.WriteString(w, r.Format)
  24. }
  25. return nil
  26. }