mirror of
https://github.com/chubin/cheat.sheets
synced 2024-11-17 09:25:32 +00:00
21 lines
427 B
Plaintext
21 lines
427 B
Plaintext
|
// loop over an array/a slice
|
||
|
for i, e := range a {
|
||
|
// i is the index, e the element
|
||
|
}
|
||
|
|
||
|
// if you only need e:
|
||
|
for _, e := range a {
|
||
|
// e is the element
|
||
|
}
|
||
|
|
||
|
// ...and if you only need the index
|
||
|
for i := range a {
|
||
|
}
|
||
|
|
||
|
// In Go pre-1.4, you'll get a compiler error if you're not using i and e.
|
||
|
// Go 1.4 introduced a variable-free form, so that you can do this
|
||
|
for range time.Tick(time.Second) {
|
||
|
// do it once a sec
|
||
|
}
|
||
|
|