Created
August 7, 2019 06:57
-
-
Save ilhamarrouf/513a4fb30ebaecaae60a7d2dadf8adb1 to your computer and use it in GitHub Desktop.
for and range provide iteration over basic data structures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
// We'll iterate over 2 values in the `queue` channel. | |
queue := make(chan string, 2) | |
queue <- "one" | |
queue <- "two" | |
close(queue) | |
// This `range` iterates over each element as it's | |
// received from `queue`. Because we `close`d the | |
// channel above, the iteration terminates after | |
// receiving the 2 elements. | |
for elem := range queue { | |
fmt.Println(elem) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment