Created
August 19, 2025 14:23
-
-
Save rabellamy/85da621ada1dd0e147c2b01230018157 to your computer and use it in GitHub Desktop.
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" | |
| "strings" | |
| "sync" | |
| ) | |
| // For this challenge you will need to implement a code that will count the frequency of each word in the textData variable. | |
| // To do that, you must consider using channels and goroutines, like producer(goroutine1) and consumer(goroutine2). | |
| // The final result will be the individual number of each word. | |
| // Pre-requisites: | |
| // 2 goroutines | |
| // 1 goroutine must be responsible to manage the text | |
| // 1 goroutine to count the words | |
| /// | |
| /// | |
| /// | |
| func main() { | |
| var wg sync.WaitGroup | |
| wg.Add(2) | |
| textData := "This is a document with some words Here are more words in another document This document repeats some words" | |
| words := make(map[string]int) | |
| ch := make(chan []string) | |
| go func() { | |
| ch <- strings.Split(textData, " ") | |
| wg.Done() | |
| }() | |
| go func() { | |
| for _, word := range <-ch { | |
| if _, ok := words[word]; !ok { | |
| words[word] = 1 | |
| } else { | |
| words[word]++ | |
| } | |
| } | |
| wg.Done() | |
| }() | |
| wg.Wait() | |
| fmt.Println(words) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment