zoukankan      html  css  js  c++  java
  • go 语言解析yaml文件作为配置文件

    1.下载第三方库:go get gopkg.in/gcfg.v1

    2.创建conf.yaml文件

    enabled: true
    path: d://

    3.解析yaml的代码

    package main
    
    import (
        "fmt"
        "gopkg.in/yaml.v2"
        "io/ioutil"
        "log"
    )
    
    func main(){
        var c conf
        c.getConf()
        fmt.Println("path:" + c.Path)
        fmt.Print(c.Enabled)
    }
    type conf struct {
        Enabled bool   `yaml:"enabled"` //yaml:yaml格式 enabled:属性的为enabled
        Path    string `yaml:"path"`
    }
    // (c *conf)这个方法可以被conf结构体调用
    func (c *conf) getConf() *conf {
        yamlFile, err := ioutil.ReadFile("conf.yaml")
        if err != nil {
            log.Printf("yamlFile.Get err   #%v ", err)
        }
    
        err = yaml.Unmarshal(yamlFile, c)
        if err != nil {
            log.Fatalf("Unmarshal: %v", err)
        }
        return c
    }
  • 相关阅读:
    爬虫基础 2.1 http原理
    爬虫基础 2.1 http原理
    3.29上午
    3.28
    3.27下午
    3.27上午
    3.24上午
    3.23下午
    3.23上午
    3.22上午
  • 原文地址:https://www.cnblogs.com/huqi96/p/14404156.html
Copyright © 2011-2022 走看看