-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
numbers = append(numbers, &i) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package main | ||
|
||
import "fmt" | ||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func main() { | ||
// mapが参照型である事の確認 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. main関数側のaddressもprintする必要がありました。 |
||
*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.
これだと他のエラーを上書きしてしまうので、
以下の様にnilで無ければ上書きする実装の方が良いと思います。
これでも
file.Close()
がエラーを出した時は元のエラーを上書きしてしまうので、本当は2つのエラーを結合する処理が必要になります。ややこしいので講義では触れていません。github.com/pkg/errors
を使った実装例