-
Notifications
You must be signed in to change notification settings - Fork 25
4回目課題実装 #23
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
base: master
Are you sure you want to change the base?
4回目課題実装 #23
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ntakashima
4回目の課題のレビューしました:pray:
@@ -6,6 +6,7 @@ func main() { | |||
|
|||
numbers := make([]*int, 0, 3) | |||
for i := 0; i < 3; i++ { | |||
i := i |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
keys := make([]int, 0, len(studnetIDMap)) | ||
for k := range studnetIDMap { | ||
keys = append(keys, k) | ||
} | ||
|
||
for k, v := range studnetIDMap { | ||
sort.Ints(keys) | ||
for _, k := range keys { | ||
// fmt.Printfでフォーマットに従った文字列を標準出力に出せる | ||
fmt.Printf("Name of StudentID:%d is %s\n", k, studnetIDMap[k]) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
fmt.Printf("the address of n is %v\n", &n) | ||
return n + 1 | ||
} | ||
func incrementWithPointer(n *int) { | ||
fmt.Printf("the address of n is %v\n", &n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
main関数側のaddressもprintする必要がありました。
@@ -24,7 +24,7 @@ func CatFile(path string) (err error) { | |||
fmt.Println("Error Handling in defer called.") | |||
} | |||
// fileはCloseする必要がある。 | |||
file.Close() | |||
err = file.Close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これだと他のエラーを上書きしてしまうので、
以下の様にnilで無ければ上書きする実装の方が良いと思います。
if closeErr := file.Close(); closeErr != nil {
err = closeErr
}
これでもfile.Close()
がエラーを出した時は元のエラーを上書きしてしまうので、本当は2つのエラーを結合する処理が必要になります。ややこしいので講義では触れていません。
github.com/pkg/errors
を使った実装例
err = errors.Wrap(err, closeErr.Error())
No description provided.