Skip to content

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

Open
wants to merge 1 commit 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: 2 additions & 2 deletions syntax/defer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func CatFile(path string) (err error) {
fmt.Println("Error Handling in defer called.")
}
// fileはCloseする必要がある。
file.Close()
err = file.Close()
Copy link
Owner

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())

}()

buf := make([]byte, 1024)
Expand All @@ -35,7 +35,7 @@ func CatFile(path string) (err error) {
}
if err != nil {
fmt.Println("File read error: ", err)
return
return err
}
fmt.Print(string(buf[:n]))
}
Expand Down
1 change: 1 addition & 0 deletions syntax/for/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ func main() {

numbers := make([]*int, 0, 3)
for i := 0; i < 3; i++ {
i := i
Copy link
Owner

Choose a reason for hiding this comment

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

👍

numbers = append(numbers, &i)
}

Expand Down
17 changes: 15 additions & 2 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 @@ -21,12 +24,22 @@ func main() {
2: "佐藤",
4: "佐々木",
}
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])
}
}

Comment on lines +27 to +38
Copy link
Owner

Choose a reason for hiding this comment

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

👍

func addMap(m map[string]string) {
m["a"] = "あ"
}

func addMapWithCopy(m map[string]string) {
copied := make(map[string]string)
for k, v := range m {
Expand Down
2 changes: 2 additions & 0 deletions syntax/pointer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ func main() {
}

func increment(n int) int {
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)
Comment on lines +21 to +25
Copy link
Owner

Choose a reason for hiding this comment

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

main関数側のaddressもprintする必要がありました。

*n++
}