mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-13 16:51:17 +00:00
first
This commit is contained in:
34
course/13-channels/exercises/4-buffered_channels/complete.go
Normal file
34
course/13-channels/exercises/4-buffered_channels/complete.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func addEmailsToQueue(emails []string) chan string {
|
||||
emailsToSend := make(chan string, len(emails))
|
||||
for _, email := range emails {
|
||||
emailsToSend <- email
|
||||
}
|
||||
return emailsToSend
|
||||
}
|
||||
|
||||
// TEST SUITE - Don't Touch Below This Line
|
||||
|
||||
func sendEmails(batchSize int, ch chan string) {
|
||||
for i := 0; i < batchSize; i++ {
|
||||
email := <-ch
|
||||
fmt.Println("Sending email:", email)
|
||||
}
|
||||
}
|
||||
|
||||
func test(emails ...string) {
|
||||
fmt.Printf("Adding %v emails to queue...\n", len(emails))
|
||||
ch := addEmailsToQueue(emails)
|
||||
fmt.Println("Sending emails...")
|
||||
sendEmails(len(emails), ch)
|
||||
fmt.Println("==========================================")
|
||||
}
|
||||
|
||||
func main() {
|
||||
test("Hello John, tell Kathy I said hi", "Whazzup bruther")
|
||||
test("I find that hard to believe.", "When? I don't know if I can", "What time are you thinking?")
|
||||
test("She says hi!", "Yeah its tomorrow. So we're good.", "Cool see you then!", "Bye!")
|
||||
}
|
||||
Reference in New Issue
Block a user