Go-yaml - Yaml parsing Toolkit For Golang

go-yaml

介绍

gopkg.in/yaml.v3 已经是个非常好用的包,但是在实际开发中总有类型转换带来的麻烦,go-yaml只是在它的基础上,简单的一层封装,作为某些类型的定义转换。

go module

go get github.com/Liangxiaowu/go-yaml

yaml文件

默认情况下会读取当前项目目录下的./configs/app.yaml文件

初始化

## 默认读取./configs/app.yaml文件
yml := New()

## 自定义yaml文件路径
yml := New(FilePath("./conf/app.yaml"))

## 自定义yaml文件,读取./configs/xxx.yaml文件
yml := New(Name("xxx.yaml"))

## 自定义yaml文件地址,读取./conf/app.yaml文件
yml := New(Dir("./conf"))

获取结构体实例

app.yaml:

user:
  name: wunder
  age: 18
  obj:
    a: 1
    b: b

main.go

## 默认是查询第层的键作为数据体
type User struct {
    Name string
    Age  int `json:"age"`
}

func getUser()  {
    var u User           # 映射结构体
    err := yml.G(&u)   # 查找user结构体
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(u)
}


## 查询其它的键的数据体
type Obj struct {
    A int
    B string `json:"a"`
}

func getUser()  {
    var o Obj                    
    err := yml.G(&o, "user")   # 指定属于哪个上层键
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(u)
}

获取指定参数值

name的值,返回是一个interface{}类型 if err != nil { fmt.Println(err) } fmt.Println(i.(string)) # 可以转换成指定的类型 }">
# G函数实现
func getName(){
    var name string
    err := New().G(&name, "user", "name")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(name)
}


# Value函数
func getValue() {
    i, err := yml.Value("user", "name") # 获取user->name的值,返回是interface{}类型
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(i.(string))             # 可以转换成指定的类型
}
Similar Resources

create a bootable disk image from Docker image or a yaml config

docker2boot docker2boot creates a bootable disk from either a Docker image or a config yaml file Features status dns Y cloud-init Y network Y ssh TODO

Oct 30, 2022

It syncronizes the configuration described in a YAML file against your GitHub Organization

It syncronizes the configuration described in a YAML file against your GitHub Organization. Combined with a CI system, it can be used to implement GitOps for GitHub.

Jul 19, 2021

YAML support for the Go language

YAML support for the Go language

YAML support for the Go language

Dec 31, 2022

Generic templating tool with support of JSON, YAML and TOML data

gotempl Small binary used to generate files from Go Templates and data files. The following formats are supported: JSON YAML TOML Usage usage: gotempl

Jun 15, 2022

Tmpl - A tool to apply variables from cli, env, JSON/TOML/YAML files to templates

tmpl allows to apply variables from JSON/TOML/YAML files, environment variables or CLI arguments to template files using Golang text/template and functions from the Sprig project.

Nov 14, 2022

SmartYAML - Go package to handle YAML

SmartYAML - Go package to handle YAML The smartyaml is a go package to handle parsed YAML files more confortable. This package is not a parser, it use

Feb 25, 2022

VINYL Inscribes Nettlesome YAML Legibly

VINYL Inscribes Nettlesome YAML Legibly VINYL formats yaml files into a canonical format retaining comments and setting the indentation by default to

Jan 18, 2022

Go-config - Config parser for go that supports environment vars and multiple yaml files

go-multiconfig This package is able to parse yaml config files. It supports gett

Jun 23, 2022

✨Clean and minimalistic environment configuration reader for Golang

Clean Env Minimalistic configuration reader Overview This is a simple configuration reading tool. It just does the following: reads and parses configu

Jan 8, 2023
A Go library for parsing struct tags from environment variables.

Envconfig Envconfig populates struct field values based on environment variables or arbitrary lookup functions. It supports pre-setting mutations, whi

Jan 2, 2023
A better way to marshal and unmarshal YAML in Golang

YAML marshaling and unmarshaling support for Go Introduction A wrapper around go-yaml designed to enable a better way of handling YAML when marshaling

Jan 4, 2023
Golang Configuration tool that support YAML, JSON, TOML, Shell Environment

Configor Golang Configuration tool that support YAML, JSON, TOML, Shell Environment (Supports Go 1.10+) Usage package main import ( "fmt" "github.c

Dec 29, 2022
Golang library for reading properties from configuration files in JSON and YAML format or from environment variables.

go-config Golang library for reading properties from configuration files in JSON and YAML format or from environment variables. Usage Create config in

Aug 22, 2022
Golang config.yaml loader

Description goconfig is a configuration library designed using the following pri

May 31, 2022
JSON or YAML configuration wrapper with convenient access methods.

Config Package config provides convenient access methods to configuration stored as JSON or YAML. This is a fork of the original version. This version

Dec 16, 2022
Library providing routines to merge and validate JSON, YAML and/or TOML files
Library providing routines to merge and validate JSON, YAML and/or TOML files

CONFLATE Library providing routines to merge and validate JSON, YAML, TOML files and/or structs (godoc) Typical use case: Make your application config

Sep 26, 2022
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.
Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

koanf (pronounced conf; a play on the Japanese Koan) is a library for reading configuration from different sources in different formats in Go applicat

Jan 8, 2023
YAML support for the Go language.

YAML support for the Go language Introduction The yaml package enables Go programs to comfortably encode and decode YAML values. It was developed with

Jan 8, 2023