Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
json example in golang
{"object":
{
"buffer_size": 10,
"Databases":
[
{
"host": "localhost",
"user": "root",
"pass": "",
"type": "mysql",
"name": "go",
"Tables":
[
{
"name": "testing",
"statment": "teststring",
"regex": "teststring ([0-9]+) ([A-z]+)",
"Types":
[
{
"id": "int",
"value": "string"
}
]
}
]
}
]
}
}
package main
/*
cat config.json
{"object":
{
"buffer_size": 10,
"Databases":
[
{
"host": "localhost",
"user": "root",
"pass": "",
"type": "mysql",
"name": "go",
"Tables":
[
{
"name": "testing",
"statment": "teststring",
"regex": "teststring ([0-9]+) ([A-z]+)",
"Types":
[
{
"id": "int",
"value": "string"
}
]
}
]
}
]
}
}
*/
import (
"fmt"
"os"
"json"
"io/ioutil"
)
type jsonobject struct {
Object ObjectType
}
type ObjectType struct {
Buffer_size int
Databases []DatabasesType
}
type DatabasesType struct {
Host string
User string
Pass string
Type string
Name string
Tables []TablesType
}
type TablesType struct {
Name string
Statment string
Regex string
Types []TypesType
}
type TypesType struct {
Id string
Value string
}
// Main function
// I realize this function is much too simple I am simply at a loss to
func main() {
file, e := ioutil.ReadFile("./config.json")
if e != nil {
fmt.Printf("File error: %v\n", e)
os.Exit(1)
}
fmt.Printf("%s\n", string(file))
//m := new(Dispatch)
//var m interface{}
var jsontype jsonobject
json.Unmarshal(file, &jsontype)
fmt.Printf("Results: %v\n", jsontype)
}
include $(GOROOT)/src/Make.inc
GOFMT=gofmt -spaces=true -tabindent=false -tabwidth=4
all:
$(GC) jsontest.go
$(LD) -o jsontest.out jsontest.$O
format:
$(GOFMT) -w jsontest.go
clean:
rm -rf *.8 *.o *.out *.6
@eugene-eeo

This comment has been minimized.

Copy link

commented Jun 29, 2014

It's import "encoding/json", not import "json".

@geeknizer

This comment has been minimized.

Copy link

commented Jan 4, 2016

It's var jsonobject jsontype instead of var jsontype jsonobject

@mattetti

This comment has been minimized.

Copy link

commented Apr 15, 2017

Note that you should be closing the file after you are done (use defer). And there are better ways to decode files if you don't want to load the entire file in memory but stream it instead.

@ghost

This comment has been minimized.

Copy link

commented Aug 16, 2017

Do we need to close the file when using ReadFile? I always thought that you only have to close the file if you used os.Open (since you would get a reference of the File (*File) that is opened), whilst ioutil.ReadFile would only return the actual data along with an error if there's an error.

But I do agree that os.Open is the preferred way of reading files.

@sahildua2305

This comment has been minimized.

Copy link

commented Dec 12, 2017

@dcefram, if you look at the implementation of the ReadFile function, it actually closes the file itself.

func ReadFile(filename string) ([]byte, error) {
	f, err := os.Open(filename)
	if err != nil {
		return nil, err
	}
	defer f.Close()
	var n int64

	if fi, err := f.Stat(); err == nil {
		if size := fi.Size(); size < 1e9 {
			n = size
		}
	}
	return readAll(f, n+bytes.MinRead)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.