-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
54 lines (48 loc) · 1.06 KB
/
json.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"strings"
)
func init() {
log.SetFlags(0)
}
func main() {
input, err := get_input()
if err != nil {
panic(err)
}
max, sorted := get_max_and_sorted(input)
output(max, sorted, input)
}
func get_input() (map[string]interface{}, error) {
b, _ := ioutil.ReadAll(os.Stdin)
input := make(map[string]interface{})
if err := json.Unmarshal(b, &input); err != nil {
return nil, err
}
return input, nil // e.g. map[string]string{"hello":"world"}
}
func get_max_and_sorted(input map[string]interface{}) (int, []string) {
max, sorted := 0, make([]string, 0, len(input))
for key := range input {
if n := len(key); n > max {
max = n
}
sorted = append(sorted, key)
}
sort.Strings(sorted)
return max, sorted // e.g. 5, []string{"hello"}
}
func output(max int, sorted []string, input map[string]interface{}) {
pad := strings.Repeat(" ", max)
fmt.Println("{")
for _, key := range sorted {
fmt.Printf("\t%s: %s%v,\n", key, pad[:len(pad)-len(key)], input[key])
}
fmt.Println("}")
}