zoukankan      html  css  js  c++  java
  • Beego学习笔记——Config

    本文为转载,原文地址:Beego学习笔记——Config

    配置文件解析

    这是一个用来解析文件的库,它的设计思路来自于database/sql,目前支持解析的文件格式有ini、json、xml、yaml,可以通过如下方式进行安装:

    go get github.com/astaxie/beego/config

    如何使用

    首先初始化一个解析器对象

    iniconf, err := NewConfig("ini", "testini.conf")
    if err != nil {
        t.Fatal(err)
    }

    然后通过对象获取数据

    iniconf.String("appname")

    解析器对象支持的函数有如下:

    Set(key, val string) error 
    String(key string) string 
    Int(key string) (int, error) 
    Int64(key string) (int64, error) 
    Bool(key string) (bool, error) 
    Float(key string) (float64, error) 
    DIY(key string) (interface{}, error)
    View Code
     
    接下来看看在我们的学习中怎么去使用这个模块,下面我们将接着上次的文章继续。
    首先我们在根目录下新建个配置文件,取名为app.conf,在上面简单写几条配置信息
    appname="beegotest"
    pageoffset=20
    然后在根目录创建个utils的文件夹,在其中增加一个go文件,名为bconfig.go,这个文件是我们用来初始化配置信息的。代码如下
    package utils
    
    import (
           "github.com/astaxie/beego/config"
           "fmt"
    )
    var BConfig config.Configer
    func init(){
           var err error
           BConfig, err = config.NewConfig("ini", "app.conf")
           if err != nil{
                  fmt.Println("config init error:", err)
           }
    }
    在这里,我们需要引入"github.com/astaxie/beego/config
            因为在我们的初始化函数中用到库中的NewConfig函数,该函数的第一个参数为adaptername, 这里我们就默认传“ini”,第二个参数为配置文件名,我们前面建的文件叫app.conf,所以这里传“app.conf”即可。
            其中BConfig便是我们初始化的Config对象,是个全局的变量,方便后面的调用。至此,我们的配置初始化便完成了。
            配置的使用就是上面提到的几个函数了。这里我们为我们定义的命令格式为: getconf -type key,其作用为获取类型为type的Key的值,另外个为setconf key val,其作用是设置key的值为val。具体函数如下:
    GetConfig
    func GetConfig(args []string)int{
           if args[1] == "-int"{
                  intConf, err := utils.BConfig.Int(args[2])
                  if err != nil{
                         fmt.Println("get type of int from config error:",err)
                  }else {
                         fmt.Println(intConf)
                  }
           }else if args[1] == "-int64"{
                  int64Conf, err := utils.BConfig.Int64(args[2])
                  if err != nil{
                         fmt.Println("get type of int64 from config error:",err)
                  }else {
                         fmt.Println(int64Conf)
                  }
           }else if args[1] == "-bool"{
                  boolConf, err := utils.BConfig.Bool(args[2])
                  if err != nil{
                         fmt.Println("get type of bool from config error:",err)
                  }else {
                         fmt.Println(boolConf)
                  }
           }else if args[1] == "-float"{
                  floatConf, err := utils.BConfig.Float(args[2])
                  if err != nil{
                         fmt.Println("get type of flaot from config error:",err)
                  }else {
                         fmt.Println(floatConf)
                  }
           }else if args[1] == "-int64"{
                  intConf, err := utils.BConfig.Int(args[2])
                  if err != nil{
                         fmt.Println("get type of int from config error:",err)
                  }else {
                         fmt.Println(intConf)
                  }
           }else {
                  fmt.Println(utils.BConfig.String(args[2]))
           }
           return 0
    }
    View Code
      
    SetConfig
    func SetConfig(args []string)int{
           err := utils.BConfig.Set(args[1], args[2])
           if err != nil{
                  fmt.Println("set config error:", err)
           }else{
                  fmt.Println("set config success")
           }
           return 0
    }
    View Code
    main.go文件中也需要做适当的调整,来调用上面两个函数。完整的代码如下:
    package main
    
    import (
           "bufio"
           "fmt"
           "os"
           "strings"
           _"beegotest/utils"
           "beegotest/utils"
    )
    
    func main() {
           r := bufio.NewReader(os.Stdin)
           handlers := GetCommandHandlers()
           Help(nil)
    
           for {
                  fmt.Print("Command> ")
                  b, _, _ := r.ReadLine()
                  line := string(b)
                  tokens := strings.Split(line, " ")
    
                  if handler, ok := handlers[tokens[0]]; ok{
                         ret := handler(tokens)
                         if ret != 0{
                                break
                         }
                  }else {
                         fmt.Println("Unknown Command:", tokens[0])
                  }
           }
    }
    
    func GetCommandHandlers() map[string]func(args []string) int {
           return map[string]func([]string) int{
                  "help": Help,
                  "h":    Help,
                  "quit" : Quit,
                  "q":Quit,
                  "getconf":GetConfig,
                  "setconf":SetConfig,
           }
    }
    
    func Help(args []string) int {
           fmt.Println(`Command:
           help(h)
           quit(q)
           getconf -type key
           setconf key val
           `)
           return 0
    }
    
    func Quit(args []string) int{
           return 1
    }
    
    func GetConfig(args []string)int{
           if args[1] == "-int"{
                  intConf, err := utils.BConfig.Int(args[2])
                  if err != nil{
                         fmt.Println("get type of int from config error:",err)
                  }else {
                         fmt.Println(intConf)
                  }
           }else if args[1] == "-int64"{
                  int64Conf, err := utils.BConfig.Int64(args[2])
                  if err != nil{
                         fmt.Println("get type of int64 from config error:",err)
                  }else {
                         fmt.Println(int64Conf)
                  }
           }else if args[1] == "-bool"{
                  boolConf, err := utils.BConfig.Bool(args[2])
                  if err != nil{
                         fmt.Println("get type of bool from config error:",err)
                  }else {
                         fmt.Println(boolConf)
                  }
           }else if args[1] == "-float"{
                  floatConf, err := utils.BConfig.Float(args[2])
                  if err != nil{
                         fmt.Println("get type of flaot from config error:",err)
                  }else {
                         fmt.Println(floatConf)
                  }
           }else if args[1] == "-int64"{
                  intConf, err := utils.BConfig.Int(args[2])
                  if err != nil{
                         fmt.Println("get type of int from config error:",err)
                  }else {
                         fmt.Println(intConf)
                  }
           }else {
                  fmt.Println(utils.BConfig.String(args[2]))
           }
           return 0
    }
    
    func SetConfig(args []string)int{
           err := utils.BConfig.Set(args[1], args[2])
           if err != nil{
                  fmt.Println("set config error:", err)
           }else{
                  fmt.Println("set config success")
           }
           return 0
    }
    View Code

    运行结果如下:



  • 相关阅读:
    几种常用的曲线
    0188. Best Time to Buy and Sell Stock IV (H)
    0074. Search a 2D Matrix (M)
    0189. Rotate Array (E)
    0148. Sort List (M)
    0859. Buddy Strings (E)
    0316. Remove Duplicate Letters (M)
    0452. Minimum Number of Arrows to Burst Balloons (M)
    0449. Serialize and Deserialize BST (M)
    0704. Binary Search (E)
  • 原文地址:https://www.cnblogs.com/ChainZhang/p/6224446.html
Copyright © 2011-2022 走看看