bird8848
V2EX  ›  Go 编程语言

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

By bird8848 at 2017 年 1 月 12 日 · 2139 次点击

先看使用代码

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 包,简单的封装了一下。然后用反射来生成配置变量,这样可以让用户定义自己的配置类型。

1 条回复  •  2017-01-12 21:53:01 +08:00
spice630
   1
spice630  
   2017 年 1 月 12 日
go 语言这么不受待见?
The Go Programming Language
› http://golang.org/
› Go Playground
Go Projects
› Revel Web Framework
© 2026 V2EX · 23ms · 3.9.8.5