GO语言: 读取配置文件的方式toml
2017-02-14 15:40
597 查看
最近go语言项目中用到读取配置文件,如下:
使用这个"github.com/BurntSushi/toml"
配置文件如下:conf.toml# id
ID = 1
# name
Name = "name1"
# weight
Weight = 1.7
# books
Books = ["a", "b", "c"]
Sex = true
#friend Friend都可以
[friend]
Age = 32
Name = "xio"代码如下 readConf.go:package conf
import (
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"os"
)
// person
type Person struct {
ID uint32
Sex bool
Name string
Weight float32
Friend *Friends
Books []string
}
// friends
type Friends struct {
Age int
Name string
}
func ReadConf(fname string) (p *Person, err error) {
var (
fp *os.File
fcontent []byte
)
p = new(Person) // &Person{}
if fp, err = os.Open(fname); err != nil {
fmt.Println("open error ", err)
return
}
if fcontent, err = ioutil.ReadAll(fp); err != nil {
fmt.Println("ReadAll error ", err)
return
}
if err = toml.Unmarshal(fcontent, p); err != nil {
fmt.Println("toml.Unmarshal error ", err)
return
}
return
}测试代码如下 readConf_test.go:package conf
import "testing"
func TestReadConf(t *testing.T) {
p, err := ReadConf("./conf.toml")
if err != nil {
t.Logf("%v", err)
}
t.Logf("person %v", p)
t.Logf("person.friend %v", p.Friend)
}输出结果为:[ `go test -v` | done: 2.6748515s ]
=== RUN TestReadConf
--- PASS: TestReadConf (0.00s)
readConf_test.go:11: person &{1 true name1 1.7 0x1218c570 [a b c]}
readConf_test.go:12: person.friend &{32 xio}
PASS
ok goProgram/conf 0.458s
细节点:
1 结构体的成员首字母大写
2 配置文件的配置项须与结构体成员名一样
3 支持bool, int, float , 字符串,字符串数组...等,也可以包含其他结构体 如[Friend]
附:学习链接
https://item.congci.com/-/content/toml-ji-jian-de-peizhiwenjian-geshi
https://github.com/toml-lang/toml
使用这个"github.com/BurntSushi/toml"
配置文件如下:conf.toml# id
ID = 1
# name
Name = "name1"
# weight
Weight = 1.7
# books
Books = ["a", "b", "c"]
Sex = true
#friend Friend都可以
[friend]
Age = 32
Name = "xio"代码如下 readConf.go:package conf
import (
"fmt"
"github.com/BurntSushi/toml"
"io/ioutil"
"os"
)
// person
type Person struct {
ID uint32
Sex bool
Name string
Weight float32
Friend *Friends
Books []string
}
// friends
type Friends struct {
Age int
Name string
}
func ReadConf(fname string) (p *Person, err error) {
var (
fp *os.File
fcontent []byte
)
p = new(Person) // &Person{}
if fp, err = os.Open(fname); err != nil {
fmt.Println("open error ", err)
return
}
if fcontent, err = ioutil.ReadAll(fp); err != nil {
fmt.Println("ReadAll error ", err)
return
}
if err = toml.Unmarshal(fcontent, p); err != nil {
fmt.Println("toml.Unmarshal error ", err)
return
}
return
}测试代码如下 readConf_test.go:package conf
import "testing"
func TestReadConf(t *testing.T) {
p, err := ReadConf("./conf.toml")
if err != nil {
t.Logf("%v", err)
}
t.Logf("person %v", p)
t.Logf("person.friend %v", p.Friend)
}输出结果为:[ `go test -v` | done: 2.6748515s ]
=== RUN TestReadConf
--- PASS: TestReadConf (0.00s)
readConf_test.go:11: person &{1 true name1 1.7 0x1218c570 [a b c]}
readConf_test.go:12: person.friend &{32 xio}
PASS
ok goProgram/conf 0.458s
细节点:
1 结构体的成员首字母大写
2 配置文件的配置项须与结构体成员名一样
3 支持bool, int, float , 字符串,字符串数组...等,也可以包含其他结构体 如[Friend]
附:学习链接
https://item.congci.com/-/content/toml-ji-jian-de-peizhiwenjian-geshi
https://github.com/toml-lang/toml
相关文章推荐
- go语言导入文件(固定长解析)(二) 增加读取json配置文件
- GO语言常用的文件读取方式
- Go语言如何读取配置文件
- go语言快速读取配置文件并声明环境变量
- 通过写xml配置文件方式实现多语言
- 读取配置文件[方式一]之纯C语言实现
- spring读取配置文件,获取bean的几种方式
- java 用单态方式实现数据库配置文件读取
- spring中读取xml配置文件、获取bean的几种方式
- java读取.properties配置文件 的几种方式
- 配置地域的信息 读取中文及其它语言文件文件时不乱码; 写utf-8格式文本文件
- java配置文件读取和项目路径的多种获取方式
- NHibernate读取配置文件的三种方式
- 读取Spring配置文件的3种方式
- 读取配置文件[方式三]之xml解析配置文件法
- Java读取XML配置文件详细总结(dom4j方式)
- spring中读取xml配置文件、获取bean的几种方式
- 关于配置文件的读取方式.
- 一起谈.NET技术,asp.net程序来读取多语言版本Ini配置文件
- spring bean的分散配置(a.properties文件读取的第四种方式)