-
-
Save primayudantra/fca87cfaa5bdabe98965319a97a63141 to your computer and use it in GitHub Desktop.
json example in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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" | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment