-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathtest_jsoniter.go
More file actions
59 lines (47 loc) · 975 Bytes
/
test_jsoniter.go
File metadata and controls
59 lines (47 loc) · 975 Bytes
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
package main
import (
"fmt"
"github.com/json-iterator/go"
"io/ioutil"
"net"
"os"
"strings"
)
type Coordinate struct {
X, Y, Z float64
}
type TestStruct struct {
Coordinates []Coordinate
}
func notify(msg string) {
conn, err := net.Dial("tcp", "localhost:9001")
if err == nil {
fmt.Fprintf(conn, msg)
conn.Close()
}
}
func main() {
bytes, err := ioutil.ReadFile("/tmp/1.json")
if err != nil {
panic(fmt.Sprintf("%v", err))
}
content := string(bytes)
reader := strings.NewReader(content)
notify(fmt.Sprintf("Go jsoniter\t%d", os.Getpid()))
// Add jsoniter
var json = jsoniter.ConfigCompatibleWithStandardLibrary
jobj := TestStruct{}
err = json.NewDecoder(reader).Decode(&jobj)
if err != nil {
panic(err)
}
x, y, z := 0.0, 0.0, 0.0
for _, coord := range jobj.Coordinates {
x += coord.X
y += coord.Y
z += coord.Z
}
len := float64(len(jobj.Coordinates))
fmt.Printf("%.8f\n%.8f\n%.8f\n", x/len, y/len, z/len)
notify("stop")
}