From 611d2bdf1b1f9eeedbba1a5d705e2c61188878d8 Mon Sep 17 00:00:00 2001 From: ookura keisuke Date: Wed, 1 Apr 2020 23:53:43 +0900 Subject: [PATCH 1/4] =?UTF-8?q?defer=E3=81=AE=E8=AA=B2=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syntax/defer/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/syntax/defer/main.go b/syntax/defer/main.go index a1da65e..617fd8b 100644 --- a/syntax/defer/main.go +++ b/syntax/defer/main.go @@ -26,7 +26,9 @@ func catFile(path string) (err error) { } // fileはCloseする必要がある。 // 本当はエラーハンドリングが必要(課題) - file.Close() + if err := file.Close(); err != nil { + fmt.Println("Error has occurred by file.Close().") + } }() // //エラーを明示的に返してdeferが呼ばれるか確認する。 From f732959617de14c360d1c3536429b15efbbbbb1c Mon Sep 17 00:00:00 2001 From: ookura keisuke Date: Wed, 1 Apr 2020 23:58:03 +0900 Subject: [PATCH 2/4] =?UTF-8?q?pointer=E3=81=AE=E8=AA=B2=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syntax/pointer/main.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/syntax/pointer/main.go b/syntax/pointer/main.go index 8da970d..61579e1 100644 --- a/syntax/pointer/main.go +++ b/syntax/pointer/main.go @@ -6,6 +6,7 @@ import ( func main() { n := 100 + fmt.Printf("origin n's pointer is %v\n", &n) // 値渡し // コピーされるので、元のnに変化はない returnValue := increment(n) @@ -18,8 +19,10 @@ func main() { } func increment(n int) int { + fmt.Printf("n's pointer is %v in increment\n", &n) return n + 1 } func incrementWithPointer(n *int) { + fmt.Printf("n's pointer is %v in incrementWithPointer\n", n) *n++ } From 515560539f57d962e821addf871958253af55c56 Mon Sep 17 00:00:00 2001 From: ookura keisuke Date: Thu, 2 Apr 2020 00:35:48 +0900 Subject: [PATCH 3/4] =?UTF-8?q?map=E3=81=AE=E8=AA=B2=E9=A1=8C2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syntax/map/main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/syntax/map/main.go b/syntax/map/main.go index b1e5bfe..2674bfc 100644 --- a/syntax/map/main.go +++ b/syntax/map/main.go @@ -1,6 +1,9 @@ package main -import "fmt" +import ( + "fmt" + "sort" +) func main() { // mapが参照型である事の確認 @@ -15,16 +18,24 @@ func main() { // --------- // mapに対するfor // 学籍番号と学生名のMap - studnetIDMap := map[int]string{ + studentIDMap := map[int]string{ 3: "田中", 1: "伊藤", 2: "佐藤", 4: "佐々木", } - for k, v := range studnetIDMap { + studentIDSlice := make([]int, len(studentIDMap)) + index := 0 + for key := range studentIDMap { + studentIDSlice[index] = key + index++ + } + sort.Slice(studentIDSlice, func(i, j int) bool { return studentIDSlice[i] < studentIDSlice[j] }) + + for _, key := range studentIDSlice { // fmt.Printfでフォーマットに従った文字列を標準出力に出せる - fmt.Printf("Name of StudentID:%d is %s\n", k, v) + fmt.Printf("Name of StudentID:%d is %s\n", key, studentIDMap[key]) } } From 96261c14952d3b9e48e0559d874dac5c146b071f Mon Sep 17 00:00:00 2001 From: ookura keisuke Date: Thu, 2 Apr 2020 00:53:50 +0900 Subject: [PATCH 4/4] =?UTF-8?q?for=E3=81=AE=E8=AA=B2=E9=A1=8C2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- syntax/for/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/syntax/for/main.go b/syntax/for/main.go index a6e91c3..66a6058 100644 --- a/syntax/for/main.go +++ b/syntax/for/main.go @@ -6,10 +6,10 @@ func main() { numbers := make([]*int, 0, 3) for i := 0; i < 3; i++ { - numbers = append(numbers, &i) + number := i + numbers = append(numbers, &number) } fmt.Println("Values:", *numbers[0], *numbers[1], *numbers[2]) fmt.Println("Addresses:", numbers[0], numbers[1], numbers[2]) - }