Created
October 23, 2021 10:48
-
-
Save minhphong306/9b024eda59d09ada043fa0a6b1ca1497 to your computer and use it in GitHub Desktop.
Gọi hàng.go
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" | |
"math/rand" | |
"time" | |
) | |
func GoiHangA(ch chan string) { | |
randTime := rand.Intn(5) | |
fmt.Printf("Mất %v giây để chờ hàng của cơ sở A tới\n", randTime) | |
time.Sleep(time.Duration(randTime) * time.Second) | |
ch <- "Hàng từ cơ sở A" | |
} | |
func GoiHangB(ch chan string) { | |
randTime := rand.Intn(5) | |
fmt.Printf("Mất %v giây để chờ hàng của cơ sở B tới\n", randTime) | |
time.Sleep(time.Duration(randTime) * time.Second) | |
ch <- "Hàng từ cơ sở B" | |
} | |
func main() { | |
coSoA := make(chan string) | |
coSoB := make(chan string) | |
go GoiHangA(coSoA) | |
go GoiHangB(coSoB) | |
var selected string | |
select { | |
case hangA := <-coSoA: | |
selected = hangA | |
case hangB := <-coSoB: | |
selected = hangB | |
} | |
fmt.Printf("Đã chọn hàng: %v\n", selected) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment