写了一个用来解析 json 格式配置文件的包 jconfig

2017-01-12 17:32:24 +08:00
 bird8848

先看使用代码

package main

import (
	"github.com/choueric/jconfig"
)

const DefContent = `{
	"server": "127.0.0.1:8088"
}
`

type Config struct {
	Server string `json:"server"`
}

func getConfig(file string) (*Config, error) {
	jc := jconfig.New(file, Config{})

	if _, err := jc.Load(DefContent); err != nil {
		return nil, err
	}

	return jc.Data().(*Config), nil
}

内部只是调用 json 包,简单的封装了一下。然后用反射来生成配置变量,这样可以让用户定义自己的配置类型。

956 次点击
所在节点    Go 编程语言
1 条回复
spice630
2017-01-12 21:53:01 +08:00
go 语言这么不受待见?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/334185

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX