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

5
package render
6

7
import (
8
	"fmt"
9
	"net/http"
10

11
	"github.com/gin-gonic/gin/internal/bytesconv"
12
)
13

14
// String contains the given interface object slice and its format.
15
type String struct {
16
	Format string
17
	Data   []interface{}
18
}
19

20
var plainContentType = []string{"text/plain; charset=utf-8"}
21

22
// Render (String) writes data with custom ContentType.
23
func (r String) Render(w http.ResponseWriter) error {
24 6
	return WriteString(w, r.Format, r.Data)
25
}
26

27
// WriteContentType (String) writes Plain ContentType.
28
func (r String) WriteContentType(w http.ResponseWriter) {
29 6
	writeContentType(w, plainContentType)
30
}
31

32
// WriteString writes data according to its format and write custom ContentType.
33
func WriteString(w http.ResponseWriter, format string, data []interface{}) (err error) {
34 6
	writeContentType(w, plainContentType)
35 6
	if len(data) > 0 {
36 6
		_, err = fmt.Fprintf(w, format, data...)
37 6
		return
38
	}
39 6
	_, err = w.Write(bytesconv.StringToBytes(format))
40 6
	return
41
}

Read our documentation on viewing source code .

Loading