3.config包使用
最后更新于:2022-04-02 04:53:09
beego config包使用:
文件目录:
~~~
|-- main.go
|-- test.conf
~~~
test.conf
~~~
;comment one
#comment two
appname = beeapi
httpport = 8080
mysqlport = 3600
PI = 3.1415976
runmode = "dev"
autorender = false
copyrequestbody = true
[demo]
key1="asta"
key2 = "xie"
CaseInsensitive = true
peers = one;two;three
~~~
main.go
~~~
package main
import (
"fmt"
"github.com/astaxie/beego/config"
)
func main() {
// 初始化一个 config 解析器对象
conf, err := config.NewConfig("ini", "test.conf")
if err != nil {
fmt.Println("new config failed, err:", err)
return
}
appname := conf.String("appname")
fmt.Printf("get appname : %v\n", appname)
httpport, err := conf.Int("httpport")
if err != nil {
fmt.Printf("get httpport err : %v\n", err)
}
fmt.Printf("get httpport : %v\n", httpport)
mysqlport, err := conf.Int64("mysqlport")
if err != nil {
fmt.Printf("get mysqlport err : %v\n", err)
}
fmt.Printf("get mysqlport : %v\n", mysqlport)
pi, err := conf.Float("PI")
if err != nil {
fmt.Printf("get PI err : %v\n", err)
}
fmt.Printf("get PI : %v\n", pi)
autorender, err := conf.Bool("autorender")
if err != nil {
fmt.Printf("get autorender err : %v\n", err)
}
fmt.Printf("get autorender : %v\n", autorender)
key1 := conf.String("demo::key1")
fmt.Printf("get key1 : %v\n", key1)
peers := conf.Strings("demo::peers")
fmt.Println("get peers : ", peers)
}
~~~
运行结果:
~~~
get appname : beeapi
get httpport : 8080
get mysqlport : 3600
get PI : 3.1415976
get autorender : false
get key1 : asta
get peers : [one two three]
~~~
';