goconfig

package module
v0.0.0-...-dc3119d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2015 License: MIT Imports: 6 Imported by: 2

README

goconfig

goconfig is a json config reader and wather for mobile game server Current version: 0.0.1


###Getting Started ####To install: go get github.com/fanyang1988/goconfig ####Base usage: this is read info

package main

import "github.com/lunny/tango"

func main() {
	config := goconfig.NewConfig()
	config.Reg("info", "../test_config_obj.json", true)
	info_data := config.Get("info")
}

this read ../test_config_obj.json as name "info" To read config data, it need to register file whit a name use Reg() function register "../test_config_obj.json" to config name And then we can get data by config name

For config in a array like this:

{
    "1" : {
        "Id" : 1,
        "Info" : "Info1"
    },
    "2" : {
        "Id" : 2,
        "Info" : "Info2"
    }
}

can use GetByIdx() to get info by index, this can use less copy

####Notify by Channel when config changed it can use RegNotifyChan to register a channel to subscribe config change event, goconfig will send name which changed to channel

func main() {
	notify_chan := make(chan string)

	config := goconfig.NewConfig()
	config.Reg("info", "../test_config_obj.json", true)

	config.RegNotifyChan(notify_chan)

	for {
		select {
		case update_name, ok := <-notify_chan:
			if !ok {
				fmt.Printf("chan closed")
				return
			}

			fmt.Printf("config update %s", update_name)
			new_info_data := config.Get(update_name)
			fmt.Printf("info_data %s", new_info_data.Get("info1"))
			return
		}
	}
}

####API API Reference

Documentation

Index

Constants

View Source
const (
	// default watcher check time inc
	DEFAULT_Update_Check_Time = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

goconfig manage

func New

func New() *Config

create goconfig manage

func (*Config) Close

func (self *Config) Close()

Close Config

func (*Config) Get

func (self *Config) Get(name string) *sj.Json

get all data from config

func (*Config) GetByIdx

func (self *Config) GetByIdx(name, idx string) *sj.Json

get data by idx and name for xlsx config

func (*Config) Reg

func (self *Config) Reg(name, path string, need_update bool) error

register an config file

name: config name
path: config file path
need_update: is need to upadte when file changed

func (*Config) RegNotifyChan

func (self *Config) RegNotifyChan(notify_chan chan string)

register a channel to get update notify for what config is changed, if "./info.json" named "info" changed, the notify_chan will recv "info"

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL