text.go 739 B

1234567891011121314151617181920212223242526272829303132333435
  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. WriteString(w, r.Format, r.Data)
  17. return nil
  18. }
  19. func WriteString(w http.ResponseWriter, format string, data []interface{}) {
  20. header := w.Header()
  21. if _, exist := header["Content-Type"]; !exist {
  22. header["Content-Type"] = plainContentType
  23. }
  24. if len(data) > 0 {
  25. fmt.Fprintf(w, format, data...)
  26. } else {
  27. io.WriteString(w, format)
  28. }
  29. }