GithubHelp home page GithubHelp logo

anuvu / yaml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gosexy/yaml

0.0 3.0 1.0 16 KB

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

Home Page: https://menteslibres.net/gosexy/yaml

Go 100.00%

yaml's Introduction

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 github.com/anuvu/yaml

Usage

After installing, use the following import path.

import "github.com/anuvu/yaml"

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

package main

import (
	"github.com/anuvu/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.

yaml's People

Contributors

xiam avatar apsdehal avatar jganapat avatar

Watchers

Ravi Chamarthy avatar James Cloos avatar  avatar

Forkers

discobean

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.