Created
November 29, 2020 16:21
-
-
Save minhphong306/4806af39131a76993ce771b565824d9a to your computer and use it in GitHub Desktop.
write_rap_song.go
This file contains 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" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
func main() { | |
rappers := []string{"Dế Choắt", "GDucky", "MCK", "TLinh", "Rtee"} | |
numBeat := 100 | |
wg := &sync.WaitGroup{} | |
beatChannel := make(chan string, 10) | |
// produceBeat | |
go produceBeat(numBeat, beatChannel) | |
// Write rap song | |
for _, rapper := range rappers { | |
wg.Add(1) | |
go writeSong(wg, rapper, beatChannel) | |
} | |
wg.Wait() | |
} | |
func writeSong(wg *sync.WaitGroup, rapper string, beatChannel chan string) { | |
defer wg.Done() | |
for { | |
beat, ok := <-beatChannel | |
if !ok { | |
fmt.Printf("Hết beat. %s ra khỏi phòng thu\n", rapper) | |
return | |
} | |
fmt.Printf("Rapper %s sử dụng beat %s\n", rapper, beat) | |
wait() | |
} | |
} | |
func produceBeat(numBeat int, beatChannel chan string) { | |
for i := 1; i <= numBeat; i++ { | |
wait() | |
fmt.Println("Tạo ra beat số: ", i) | |
beatChannel <- fmt.Sprintf("Beat %d", i) | |
} | |
close(beatChannel) | |
} | |
func wait() { | |
time.Sleep(time.Second * time.Duration(rand.Intn(2))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment