...
1
2
3
4
5 package render
6
7 import (
8 "fmt"
9 "net/http"
10
11 "github.com/gin-gonic/gin/internal/bytesconv"
12 )
13
14
15 type String struct {
16 Format string
17 Data []any
18 }
19
20 var plainContentType = []string{"text/plain; charset=utf-8"}
21
22
23 func (r String) Render(w http.ResponseWriter) error {
24 return WriteString(w, r.Format, r.Data)
25 }
26
27
28 func (r String) WriteContentType(w http.ResponseWriter) {
29 writeContentType(w, plainContentType)
30 }
31
32
33 func WriteString(w http.ResponseWriter, format string, data []any) (err error) {
34 writeContentType(w, plainContentType)
35 if len(data) > 0 {
36 _, err = fmt.Fprintf(w, format, data...)
37 return
38 }
39 _, err = w.Write(bytesconv.StringToBytes(format))
40 return
41 }
42
View as plain text