text.go 619 B

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