github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/gin/render/text.go (about) 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 "github.com/hellobchain/newcryptosm/http" 10 11 "github.com/hellobchain/third_party/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 return WriteString(w, r.Format, r.Data) 25 } 26 27 // WriteContentType (String) writes Plain ContentType. 28 func (r String) WriteContentType(w http.ResponseWriter) { 29 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 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 }