Skip to content

Commit c2e07e7

Browse files
committed
Update
1 parent f878a36 commit c2e07e7

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

examples/basic/loops/diff_rangeloop_and_normalloop.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ import "github.com/devlights/gomy/output"
44

55
// DiffRangeLoopAndNormalLoop は、 range ループと通常のループの違いについてのサンプルです.
66
func DiffRangeLoopAndNormalLoop() error {
7-
var (
8-
s1 = []int{1, 2, 3}
9-
)
10-
117
//
128
// range ループは、ループ時に元の値のコピーを作成してそれをループする。
139
// 以下の処理は、ループの度に元のスライスに要素を追加しているが
1410
// rangeループでコピーしたスライスは変化が無いため、ループは3回で終了し
1511
// 元のスライスの要素数も増えて6となる。
1612
//
13+
var (
14+
s1 = []int{1, 2, 3}
15+
)
1716
for range s1 { // ループが始まる直前に内部的にコピーが取られ、そのコピーを元にループするイメージ
1817
s1 = append(s1, 99)
1918
}

0 commit comments

Comments
 (0)