1 Star 0 Fork 9

githubvip / go配置文件解析器

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
write.go 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
shirdon 提交于 2019-07-07 15:08 . 上传
//++++++++++++++++++++++++++++++++++++++++
//Fighting for great,share generate value!
//Build the best soft by golang,let's go!
//++++++++++++++++++++++++++++++++++++++++
//Author:ShirDon <http://www.shirdon.com>
//Email:hcbsts@163.com; 823923263@qq.com
//++++++++++++++++++++++++++++++++++++++++
package config
import (
"bufio"
"fmt"
"os"
"strings"
)
// WriteFile saves the configuration representation to a file.
// The desired file permissions must be passed as in os.Open. The header is a
// string that is saved as a comment in the first line of the file.
func (c *Config) WriteFile(fname string, perm os.FileMode, header string) error {
file, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}
buf := bufio.NewWriter(file)
if err = c.write(buf, header); err != nil {
return err
}
buf.Flush()
return file.Close()
}
func (c *Config) write(buf *bufio.Writer, header string) (err error) {
if header != "" {
// Add comment character after of each new line.
if i := strings.Index(header, "\n"); i != -1 {
header = strings.Replace(header, "\n", "\n"+c.comment, -1)
}
if _, err = buf.WriteString(c.comment + header + "\n"); err != nil {
return err
}
}
for _, orderedSection := range c.Sections() {
for section, sectionMap := range c.data {
if section == orderedSection {
// Skip default section if empty.
if section == DEFAULT_SECTION && len(sectionMap) == 0 {
continue
}
if _, err = buf.WriteString("\n[" + section + "]\n"); err != nil {
return err
}
// Follow the input order in options.
for i := 0; i < c.lastIdOption[section]; i++ {
for option, tValue := range sectionMap {
if tValue.position == i {
if len(tValue.vMuti) > 0 {
for _, realValue := range tValue.vMuti {
if _, err = buf.WriteString(fmt.Sprint(
"@", option, c.separator, realValue, "\n")); err != nil {
return err
}
}
} else {
if _, err = buf.WriteString(fmt.Sprint(
option, c.separator, tValue.v, "\n")); err != nil {
return err
}
}
c.RemoveOption(section, option)
break
}
}
}
}
}
}
if _, err = buf.WriteString("\n"); err != nil {
return err
}
return nil
}
Go
1
https://gitee.com/githubvip/configs_witch_go.git
git@gitee.com:githubvip/configs_witch_go.git
githubvip
configs_witch_go
go配置文件解析器
master

搜索帮助