magiconair/properties
GitHub: magiconair/properties
Go 语言的 Java properties 配置文件解析与生成库,支持递归变量展开和结构体解码。
Stars: 340 | Forks: 80
[](https://github.com/magiconair/properties/releases)
[](https://raw.githubusercontent.com/magiconair/properties/master/LICENSE)
[](http://godoc.org/github.com/magiconair/properties)
# 概述
properties 是一个用于读取和写入 properties 文件的 Go 库。
它支持从多个文件或 URL 读取,并支持类似 `${key}` 表达式的 Spring 风格递归
属性展开为对应的值。
值表达式可以引用其他键(如 `${key}`)或环境
变量(如 `${USER}`)。文件名也可以包含环境变量
例如 `/home/${USER}/myapp.properties`。
可以通过
struct tag 将 properties 解码为结构体、map、数组或值。
注释和键的顺序会被保留。注释可以被修改
并写入到输出中。
properties 库支持 ISO-8859-1 和 UTF-8 编码的数据。
从 1.3.0 版本开始,MustXXX() 函数的行为
可以通过提供自定义的 `ErrorHandler` 函数来配置。默认值已
从 `panic` 更改为 `log.Fatal`,但这是可配置的,并且可以提供自定义的
错误处理函数。详见包文档
了解详情。
在 [](http://godoc.org/github.com/magiconair/properties) 上阅读完整文档
## 快速入门
```
import (
"flag"
"github.com/magiconair/properties"
)
func main() {
// init from a file
p := properties.MustLoadFile("${HOME}/config.properties", properties.UTF8)
// or multiple files
p = properties.MustLoadFiles([]string{
"${HOME}/config.properties",
"${HOME}/config-${USER}.properties",
}, properties.UTF8, true)
// or from a map
p = properties.LoadMap(map[string]string{"key": "value", "abc": "def"})
// or from a string
p = properties.MustLoadString("key=value\nabc=def")
// or from a URL
p = properties.MustLoadURL("http://host/path")
// or from multiple URLs
p = properties.MustLoadURL([]string{
"http://host/config",
"http://host/config-${USER}",
}, true)
// or from flags
p.MustFlag(flag.CommandLine)
// get values through getters
host := p.MustGetString("host")
port := p.GetInt("port", 8080)
// or through Decode
type Config struct {
Host string `properties:"host"`
Port int `properties:"port,default=9000"`
Accept []string `properties:"accept,default=image/png;image;gif"`
Timeout time.Duration `properties:"timeout,default=5s"`
}
var cfg Config
if err := p.Decode(&cfg); err != nil {
log.Fatal(err)
}
}
```
## 安装与升级
```
$ go get -u github.com/magiconair/properties
```
## 许可证
2 条款 BSD 许可证。详情请参阅 [LICENSE](https://github.com/magiconair/properties/blob/master/LICENSE) 文件。
## 待办事项
* 导出内容时将密码和机密信息进行掩码处理
标签:EVTX分析, Go, Java Properties, Ruby工具, 开发库, 日志审计, 解析器