/
util.go
65 lines (50 loc) · 1.41 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
)
/*
Utility modules for supporting rest API
Note:
1. Variables with lowercase starting letter are private.
2. Variables with uppercase starting letter are exported or public.
*/
func init() {
fmt.Println("Utility function loaded")
fmt.Println("=======================")
fmt.Println("Total CPUs in this machine :", runtime.GOMAXPROCS(runtime.NumCPU()))
fmt.Println()
}
// GetJSONArrayData (Address of file in string form)
// function to get all the data from json file
// exported data is in the form of array of objects
func GetJSONArrayData(FileAddr string) []map[string]interface{} {
// open the JSON file
jsonFile, err := os.Open(FileAddr)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result []map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
return result
}
// GetJSONObjectData (Address of file in string form)
// function to get all the data from json file
// exported data is in the form of object map[string]interface{}
func GetJSONObjectData(FileAddr string) map[string]interface{} {
// open the JSON file
jsonFile, err := os.Open(FileAddr)
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]interface{}
json.Unmarshal([]byte(byteValue), &result)
return result
}