cwchen icon

[Go] Concurrent generators.

cwchen | PRO | 11/29/17 01:22:44 AM UTC | 0 ⭐ | 1573 👁️ | Never ⏰ | []
Go |

730 B

|

None

|

0 👍

/

0 👎

package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    data := []string{
        "The yellow fish swims slowly in the water",
        "The brown dog barks loudly after a drink from its water bowl",
        "The dark bird of prey lands on a small tree after hunting for fish",
    }
 
    histogram := make(map[string]int)
    wordsCh := make(chan string)
 
    go func() {
        defer close(wordsCh)
 
        for _, line := range data {
            words := strings.Split(line, " ")
 
            for _, word := range words {
                word = strings.ToLower(word)
                wordsCh <- word
            }
        }
    }()
 
    for {
        word, opened := <- wordsCh
        if !opened {
            break
        }
        histogram[word]++
    }
 
    for k, v := range histogram {
        fmt.Println(fmt.Sprintf("%s\t(%d)", k, v))
    }
}

Comments