-
Notifications
You must be signed in to change notification settings - Fork 25
第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
base: master
Are you sure you want to change the base?
第4回の課題(大倉) #18
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,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
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. 👍 |
||
} | ||
|
||
fmt.Println("Values:", *numbers[0], *numbers[1], *numbers[2]) | ||
fmt.Println("Addresses:", numbers[0], numbers[1], numbers[2]) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
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. mapからslice作れんかなぁと思ったのですが、無理っぽかったので愚直に詰めました 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. 自分の知る限りそういうメソッドはないので、自前で変換する必要があります: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
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. 👍 |
||
// 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]) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
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. 👍 |
||
*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.
ここのエラーを呼び出し元に返す場合は、第5回の解説で説明した通り以下の様な実装にするのが良いです。
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.
なるほど