Skip to content
forked from gosexy/yaml

Wraps goyaml functions for easier use of YAML files as configuration sources.

Notifications You must be signed in to change notification settings

esilva-everbridge/yaml

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gosexy/yaml

This package is a wrapper of goyaml.

gosexy/yaml provides friendly methods for loading, reading and writing to and from YAML formatted files.

Useful if you just want to read/write setting files in your Go programs.

Installation

go get menteslibres.net/gosexy/yaml

Usage

After installing, use the following import path.

import "menteslibres.net/gosexy/yaml"

Here's an example that creates a YAML file and writes some values on it:

package main

import (
	"menteslibres.net/gosexy/yaml"
)

func main() {
	settings := yaml.New()

	settings.Set("success", true)
	settings.Set("nested", "tree", 1)
	settings.Set("another", "nested", "tree", []int{1, 2, 3})

	settings.Write("test.yaml")
}

The above code generates a test.yaml file like this:

another:
  nested:
    tree:
    - 1
    - 2
    - 3
nested:
  tree: 1
success: true

A detail of another example:

settings, err := yaml.Open("examples/input/settings.yaml")

if err != nil {
	log.Printf("Could not open YAML file: %s", err.Error())
}

s := to.String(settings.Get("test_string"))

fmt.Printf("%s", s)

If the referred settings.yaml contains a line like this:

test_string: "Hello World!"

The the output would be:

Hello World!

Note that you can use nested paths on Set() and Get():

/*
path:
	to:
		nested:
			value: 1
*/
settings.Set("path", "to", "nested", "value", 1)

// yaml.*Yaml.Get() returns interface{}.
i := settings.Get("path", "to", "nested", "value")

fmt.Printf("%d\n", i)
// Prints: 1

You can also use gosexy/to to convert from interface{} into a compatible type:

// to.Int64() returns int64.
i := to.Int64(settings.Get("path", "to", "nested", "value"))

fmt.Printf("%d\n", i)
// Prints: 1

Documentation

See the online docs.

About

Wraps goyaml functions for easier use of YAML files as configuration sources.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%