Comment on page
YAML
Content-Type: "application/x-yaml"
YAML (a recursive acronym for "YAML Ain't Markup Language") is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Extensible Markup Language (XML) but has a minimal syntax which intentionally differs from SGML.
The
Context.YAML(v)
is the method which sends YAML responses to the client. It accepts a value of any type. You only need the yaml
struct field and all fields should be exported.type ExampleYAML struct {
Name string `yaml:"name"`
ServerAddr string `yaml:"ServerAddr"`
}
func handler(ctx iris.Context) {
response := ExampleYAML{Name: "Iris", ServerAddr: "localhost:8080"}
ctx.YAML(response)
}
Result
name: Iris
ServerAddr: localhost:8080
The same result can be achieved using
iris.Map
or a standard Go map
:func handler(ctx iris.Context) {
response := iris.Map{"name": "Iris", "serverAddr": "localhost:8080"}
ctx.YAML(response)
}
References:
Last modified 8mo ago