Skip to content
Myr

text.go

This file

Path

render/text.go

41 lines · 1.1 KiB · Go

Last changed by

Not known.

History of this file

Take it

1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package render67import (8	"fmt"9	"net/http"1011	"github.com/gin-gonic/gin/internal/bytesconv"12)1314// String contains the given interface object slice and its format.15type String struct {16	Format string17	Data   []any18}1920var plainContentType = []string{"text/plain; charset=utf-8"}2122// Render (String) writes data with custom ContentType.23func (r String) Render(w http.ResponseWriter) error {24	return WriteString(w, r.Format, r.Data)25}2627// WriteContentType (String) writes Plain ContentType.28func (r String) WriteContentType(w http.ResponseWriter) {29	writeContentType(w, plainContentType)30}3132// WriteString writes data according to its format and write custom ContentType.33func 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		return38	}39	_, err = w.Write(bytesconv.StringToBytes(format))40	return41}