Skip to content

第4回の課題(大倉) #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion syntax/defer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().")
}
Comment on lines +29 to +31
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここのエラーを呼び出し元に返す場合は、第5回の解説で説明した通り以下の様な実装にするのが良いです。

if closeErr := file.Close(); closeErr != nil {
	err = closeErr
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど

}()

// //エラーを明示的に返してdeferが呼ばれるか確認する。
Expand Down
4 changes: 2 additions & 2 deletions syntax/for/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +9 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

fmt.Println("Values:", *numbers[0], *numbers[1], *numbers[2])
fmt.Println("Addresses:", numbers[0], numbers[1], numbers[2])

}
19 changes: 15 additions & 4 deletions syntax/map/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "fmt"
import (
"fmt"
"sort"
)

func main() {
// mapが参照型である事の確認
Expand All @@ -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))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapからslice作れんかなぁと思ったのですが、無理っぽかったので愚直に詰めました

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分の知る限りそういうメソッドはないので、自前で変換する必要があります:pray:

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 {
Comment on lines +29 to +36
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// 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])
}
}

Expand Down
3 changes: 3 additions & 0 deletions syntax/pointer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

func main() {
n := 100
fmt.Printf("origin n's pointer is %v\n", &n)
// 値渡し
// コピーされるので、元のnに変化はない
returnValue := increment(n)
Expand All @@ -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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

*n++
}