From 7539f68f08cf40aa4a72342c2ce0959973dd8abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 12:36:21 +0900 Subject: [PATCH 1/7] initial commit --- go/study/ch04_json/go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go/study/ch04_json/go.mod diff --git a/go/study/ch04_json/go.mod b/go/study/ch04_json/go.mod new file mode 100644 index 00000000..198c7459 --- /dev/null +++ b/go/study/ch04_json/go.mod @@ -0,0 +1,3 @@ +module github.com/tecyokomichi/WebAppLearning/go/study/ch04_json + +go 1.19 From 6015063c392a9745a7389494b22b3cec2f5b1579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 13:05:36 +0900 Subject: [PATCH 2/7] =?UTF-8?q?:+1:=20=E3=82=B5=E3=83=96=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=20example=20=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch04_json/main.go | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 go/study/ch04_json/main.go diff --git a/go/study/ch04_json/main.go b/go/study/ch04_json/main.go new file mode 100644 index 00000000..a40f2a0d --- /dev/null +++ b/go/study/ch04_json/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" +) + +type Person struct { + FirstName string + LastName string + Birthday string + Age int +} + +func main() { + if len(os.Args) < 2 { + showHelp() + os.Exit(1) + } + switch os.Args[1] { + case "example": + person := &Person{ + FirstName: "Blake", + LastName: "Serild", + Birthday: "1989-07-10", + Age: 33, + } + + b, err := json.Marshal(person) + if err != nil { + fmt.Printf("Error: %v", err) + os.Exit(1) + } + fmt.Println(string(b)) + default: + showHelp() + os.Exit(1) + } +} + +func showHelp() { + fmt.Printf("Usage:\n") + fmt.Printf("%s example\n", os.Args[0]) + fmt.Printf("Shows an example of JSON data\n") +} \ No newline at end of file From e796dc5a6ec7b88f99f9ffe4e9238f18a53ad4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 13:08:22 +0900 Subject: [PATCH 3/7] =?UTF-8?q?:+1:=20=E6=95=B4=E5=BD=A2=E3=81=97=E3=81=A6?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch04_json/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/study/ch04_json/main.go b/go/study/ch04_json/main.go index a40f2a0d..2f8f7e16 100644 --- a/go/study/ch04_json/main.go +++ b/go/study/ch04_json/main.go @@ -27,7 +27,7 @@ func main() { Age: 33, } - b, err := json.Marshal(person) + b, err := json.MarshalIndent(person, "", "\t") if err != nil { fmt.Printf("Error: %v", err) os.Exit(1) From c6af76a9a90bbe61f738adad2199878a983cccd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 13:15:07 +0900 Subject: [PATCH 4/7] =?UTF-8?q?:+1:=20people=20=E3=82=92=E5=87=BA=E5=8A=9B?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch04_json/main.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/go/study/ch04_json/main.go b/go/study/ch04_json/main.go index 2f8f7e16..c20790e3 100644 --- a/go/study/ch04_json/main.go +++ b/go/study/ch04_json/main.go @@ -20,14 +20,28 @@ func main() { } switch os.Args[1] { case "example": - person := &Person{ - FirstName: "Blake", - LastName: "Serild", - Birthday: "1989-07-10", - Age: 33, + people := []*Person { + { + FirstName: "Blake", + LastName: "Serild", + Birthday: "1989-07-10", + Age: 33, + }, + { + FirstName: "Libbie", + LastName: "Drisko", + Birthday: "1988-06-15", + Age: 24, + }, + { + FirstName: "Anestassia", + LastName: "Truc", + Birthday: "1973-04-02", + Age: 48, + }, } - b, err := json.MarshalIndent(person, "", "\t") + b, err := json.MarshalIndent(people, "", "\t") if err != nil { fmt.Printf("Error: %v", err) os.Exit(1) From ae968c8d4d986083f61d24fc7bad61ee37ab4a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 13:24:03 +0900 Subject: [PATCH 5/7] :pencil2: add people.json --- go/study/ch04_json/people.json | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 go/study/ch04_json/people.json diff --git a/go/study/ch04_json/people.json b/go/study/ch04_json/people.json new file mode 100644 index 00000000..f46a81d4 --- /dev/null +++ b/go/study/ch04_json/people.json @@ -0,0 +1,32 @@ +[ + { + "first_name": "Blake", + "last_name": "Serilda", + "birthday": "1989-07-10", + "age": 33 + }, + { + "first_name": "Libbie", + "last_name": "Drisko", + "birthday": "1998-06-15", + "age": 24 + }, + { + "first_name": "Anestassia", + "last_name": "Truc", + "birthday": "1973-04-02", + "age": 48 + }, + { + "first_name": "Cyndie", + "last_name": "Syd", + "birthday": "1985-07-12", + "age": 37 + }, + { + "first_name": "Joelly", + "last_name": "Honoria", + "birthday": "2012-01-31", + "age": 10 + } +] From 9feb8b5bebfe98a60063e17e4a9ba4527fdfb139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 13:42:06 +0900 Subject: [PATCH 6/7] =?UTF-8?q?:sparkles:=20=E3=82=BF=E3=82=B0=E4=BB=98?= =?UTF-8?q?=E3=81=91=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch04_json/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/study/ch04_json/main.go b/go/study/ch04_json/main.go index c20790e3..a825873c 100644 --- a/go/study/ch04_json/main.go +++ b/go/study/ch04_json/main.go @@ -7,10 +7,10 @@ import ( ) type Person struct { - FirstName string - LastName string - Birthday string - Age int + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Birthday string `json:"birthday"` + Age int `json:"age"` } func main() { From 61ebee231d8652710bcb3c51457550ed238c9eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Wed, 2 Nov 2022 13:49:21 +0900 Subject: [PATCH 7/7] =?UTF-8?q?:+1:=20=E3=82=B5=E3=83=96=E3=82=B3=E3=83=9E?= =?UTF-8?q?=E3=83=B3=E3=83=89=20summary=20=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch04_json/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/go/study/ch04_json/main.go b/go/study/ch04_json/main.go index a825873c..b4dc7b93 100644 --- a/go/study/ch04_json/main.go +++ b/go/study/ch04_json/main.go @@ -47,6 +47,28 @@ func main() { os.Exit(1) } fmt.Println(string(b)) + case "summary": + var people []*Person + if len(os.Args) < 3 || len(os.Args) > 3 { + showHelp() + os.Exit(1) + } + f, err := os.ReadFile(os.Args[2]) + if err != nil { + fmt.Printf("Error: %v", err) + os.Exit(1) + } + err = json.Unmarshal(f, &people) + if err != nil { + fmt.Printf("Error: %v", err) + os.Exit(1) + } + count, sum := 0, 0 + for _, p := range people { + count++ + sum += p.Age + } + fmt.Printf("%d 人、平均年齢 %d \n", count, sum/count) default: showHelp() os.Exit(1)