Created
October 23, 2021 11:16
-
-
Save minhphong306/7554a54c9ca5b743eab9de436de1a10f to your computer and use it in GitHub Desktop.
Gọi hàng 2.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 | |
default: | |
selected = "Không có hàng" | |
} | |
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