本文爲轉載,原文地址:Beego學習筆記——Configgit
這是一個用來解析文件的庫,它的設計思路來自於database/sql
,目前支持解析的文件格式有ini、json、xml、yaml,能夠經過以下方式進行安裝:github
go get github.com/astaxie/beego/config
首先初始化一個解析器對象sql
iniconf, err := NewConfig("ini", "testini.conf") if err != nil { t.Fatal(err) }
而後經過對象獲取數據json
iniconf.String("appname")
解析器對象支持的函數有以下:app
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)
appname="beegotest" pageoffset=20
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) } }
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 }
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 }
運行結果以下:ide