yaml

package module
v0.0.0-...-6df1e56 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 7 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference Code Coverage License

This repository provides functionality for YAML marshalling and unmarshalling similar to JSON v2. It allows users to use the same JSON options they would use for JSON v2 to encode and decode YAML.

Features

  • Marshal Go structs to YAML
  • Unmarshal YAML to Go structs
  • Support for JSON v2 options
  • Strings that would read back as another type, such as "true" or "42", are quoted, so a round trip keeps them strings

Usage

To install the package, use the following command:

go get github.com/MarkRosemaker/yaml

Here is a basic example of how to use the package:

package main

import (
	"fmt"
	"github.com/MarkRosemaker/yaml"
)

type Example struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

func main() {
	ex := Example{Name: "John Doe", Age: 30}

	// Marshal to YAML
	yamlData, err := yaml.Marshal(ex)
	if (err != nil) {
		fmt.Println("Error marshalling to YAML:", err)
		return
	}
	fmt.Println("YAML Data:", string(yamlData))

	// Unmarshal from YAML
	var ex2 Example
	err = yaml.Unmarshal(yamlData, &ex2)
	if (err != nil) {
		fmt.Println("Error unmarshalling from YAML:", err)
		return
	}
	fmt.Println("Unmarshalled Struct:", ex2)
}

JSON v2 Options

This package supports the same options available in JSON v2 for encoding and decoding. You can use json struct tags to customize the YAML output just as you would with JSON.

Additional Information

Contributing

Contributions are welcome — please open an issue or a pull request on GitHub.

License

This project is licensed under the Apache 2.0 License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(in any, opts ...json.Options) ([]byte, error)

Marshal serializes a Go value as a []byte according to the provided marshal and encode options (while ignoring unmarshal or decode options).

func Unmarshal

func Unmarshal(in []byte, out any, opts ...json.Options) error

Unmarshal decodes a []byte input into a Go value according to the provided unmarshal and decode options (while ignoring marshal or encode options). The output must be a non-nil pointer.

func UnmarshalRead

func UnmarshalRead(in io.Reader, out any, opts ...json.Options) error

UnmarshalRead deserializes a Go value from an io.Reader according to the provided unmarshal and decode options (while ignoring marshal or encode options). It consumes the entirety of io.Reader until io.EOF is encountered, without reporting an error for EOF. The output must be a non-nil pointer. See Unmarshal for details about the conversion of JSON into a Go value.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址