gojsonconfig

command module
v0.0.0-...-a4987fb Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: GPL-3.0 Imports: 4 Imported by: 0

README

Reading Json Configuration by Golang

As we all know, go language gives us a simple library to read json file. However, when it comes to more complex json file it is weak,so we want to give you a stronger and more robust way to get json value.

How to use it

中文版本

  • Use command below to install package gojsonconfig
go mod -u "https://github.com/xieyuschen/json-to-go-types"
  • You can use command gojsonconfig -v to check whether you install this package successfully or not. If you are successful, you will see output as this at terminal.
$ json-to-go-types -v
v0.1
  • Generate a struct based on json file for further reading Firstly your current path needs to contain a json file named config.json. Secondly your current path needs to have a folder called models where the generated struct model will be stored. If your path has no folder named models,please create one first.Your path dictionary would like this:
$-->your current postion
|__config,json
|__models(This is a folder)

Do command below after all of thing before done.

json-to-go-types -g

After this you can find a Config_gen.go file in models dictionary which is a struct based on the config.json.

  • You can read json to the model generated before like this:
func main(){
	conf:=ReadSettingsFromFile("config.json")
	fmt.Println(config.DbSettings.Password)
}
func ReadSettingsFromFile(settingFilePath string)(config models.Config){
	jsonFile, err := os.Open(settingFilePath)
	if err != nil {
		fmt.Println(err)
	}
	defer jsonFile.Close()
	byteValue, _ := ioutil.ReadAll(jsonFile)
	err = json.Unmarshal(byteValue, &config)
	if err != nil {
		log.Panic(err)
	}
	return config
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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