golang读写yaml

==yaml内容==

gin_mode: release
authors:
- "111"
- "222"
age: 18
dev:
  password: 123
test:
  password: 88

 

==读写程序==

package main

import (
    "fmt"
    "gopkg.in/yaml.v2"
    "io/ioutil"
)

type Pwd struct {
    Password int `yaml:"password"`
}
type Env struct {
    GinMode string   `yaml:"gin_mode"`
    Authors []string `yaml:"authors"`
    Age     int      `yaml:"age"`
    Dev     Pwd      `yaml:"dev"`
    Test    Pwd      `yaml:"test"`
}

func main() {
    path := "yaml/test.yaml"

    // 读取yaml
    env := readYaml(path)

    // 修改值
    env.Age = 18
    env.Test.Password = 88

    // 写入yaml
    writeYaml(path, env)
}

func checkError(err error) {
    if err != nil {
        panic(err)
    }
}

func readYaml(path string) (env Env) {
    content, err := ioutil.ReadFile(path)
    checkError(err)

    err = yaml.Unmarshal(content, &env)
    checkError(err)

    fmt.Println(err, env)
    return env
}

func writeYaml(path string, env Env) {
    data, err := yaml.Marshal(env)
    checkError(err)

    err = ioutil.WriteFile(path, data, 0777)
    checkError(err)
}

 

posted @ 2022-04-07 13:46  大墨垂杨  阅读(208)  评论(0编辑  收藏  举报