V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
jxia
V2EX  ›  Go 编程语言

gookit/properties - Java Properties 格式内容解析、编码和解码库

  •  1
     
  •   jxia · 2022-08-25 14:19:29 +08:00 · 751 次点击
    这是一个创建于 617 天前的主题,其中的信息可能已经有所发展或是发生改变。

    gookit/properties - GO 实现的 Java Properties 格式内容解析、编码和解码库

    • 通用的 Java properties 格式内容解析器
    • 支持像 json 包一样的 MarshalUnmarshal
    • 支持以 !, # 开头的行注释
      • 增强: 也额外支持 //, /* multi line comments */
    • 支持多行字符串值,以 \\ 结尾进行换行
      • 增强: 也额外支持 '''multi line string'''', """multi line string"""
    • 支持值引用 var 解析。format: ${some.other.key}
    • 支持 ENV 变量解析。format: ${APP_ENV}, ${APP_ENV | default}

    Github: https://github.com/gookit/properties

    使用示例

    解析并绑定到结构体

    package main
    
    import (
    	"fmt"
    	"github.com/gookit/properties"
    )
    
    func Example() {
    	text := `
    # properties string
    name = inhere
    age = 200
    `
    	p, err := properties.Parse(text)
    	if err != nil {
    		panic(err)
    	}
    	type MyConf struct {
    		Name string `properties:"name"`
    		Age  int    `properties:"age"`
    	}
    	cfg := &MyConf{}
    	err = p.MapStruct("", cfg)
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(*cfg)
    	// Output:
    	// {inhere 200}
    }
    

    Github: https://github.com/gookit/properties

    2 条回复    2022-08-31 11:22:42 +08:00
    randm
        1
    randm  
       2022-08-25 15:09:28 +08:00
    已 star ,楼主的工具库挺好用的
    jxia
        2
    jxia  
    OP
       2022-08-31 11:22:42 +08:00
    @randm O(∩_∩)O 哈哈~ 感谢支持
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1250 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:17 · PVG 01:17 · LAX 10:17 · JFK 13:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.