-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlooping04.go
More file actions
33 lines (25 loc) · 911 Bytes
/
looping04.go
File metadata and controls
33 lines (25 loc) · 911 Bytes
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
/* Alta3 Research | RZFeeser
for-each range loop */
package main
import "fmt"
func main() {
// create a string slice
a := []string{"Alta3", "Research", "Loves", "GoLang"}
// the "i" position is always the index (position)
// the "s" position is always the corresponding value
// the second iteration variable ("s") is optional
for i, s := range a {
fmt.Println("Position", i, "contains the string:", s)
}
// in this example, we removed the second iteration variable ("s")
// the "i" position is always the index (position)
for i := range a {
fmt.Println("Position", i)
}
// the name of the variable we use in the for-loop is not relevant
// however, best practice says that all variables should be named in a
// manner that is easy to understand
for pos := range a {
fmt.Println("Position", pos)
}
}