forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample4.go
47 lines (35 loc) · 1.07 KB
/
example4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Sample program to show how the for range has both value and pointer semantics.
package main
import "fmt"
func main() {
// Using the pointer semantic form of the for range.
friends := [5]string{"Annie", "Betty", "Charley", "Doug", "Edward"}
fmt.Printf("Bfr[%s] : ", friends[1])
for i := range friends {
friends[1] = "Jack"
if i == 1 {
fmt.Printf("Aft[%s]\n", friends[1])
}
}
// Using the value semantic form of the for range.
friends = [5]string{"Annie", "Betty", "Charley", "Doug", "Edward"}
fmt.Printf("Bfr[%s] : ", friends[1])
for i, v := range friends {
friends[1] = "Jack"
if i == 1 {
fmt.Printf("v[%s]\n", v)
}
}
// Using the value semantic form of the for range but with pointer
// semantic access. DON'T DO THIS.
friends = [5]string{"Annie", "Betty", "Charley", "Doug", "Edward"}
fmt.Printf("Bfr[%s] : ", friends[1])
for i, v := range &friends {
friends[1] = "Jack"
if i == 1 {
fmt.Printf("v[%s]\n", v)
}
}
}