【Gin-v1.9.0源码阅读】render/yaml.go

// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.

package render

import (
	"net/http"

	"gopkg.in/yaml.v3"
)

// YAML contains the given interface object.
// YAML包含给定的接口对象。
type YAML struct {
	Data any
}

var yamlContentType = []string{"application/x-yaml; charset=utf-8"}

// Render (YAML) marshals the given interface object and writes data with custom ContentType.
// Render(YAML)封送给定的接口对象,并使用自定义ContentType写入数据。
func (r YAML) Render(w http.ResponseWriter) error {
	r.WriteContentType(w)

	bytes, err := yaml.Marshal(r.Data)
	if err != nil {
		return err
	}

	_, err = w.Write(bytes)
	return err
}

// WriteContentType (YAML) writes YAML ContentType for response.
// WriteContentType(YAML)为响应写入YAML ContentType。
func (r YAML) WriteContentType(w http.ResponseWriter) {
	writeContentType(w, yamlContentType)
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/131059569
今日推荐