Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Created October 23, 2021 11:16
Show Gist options
  • Save minhphong306/7554a54c9ca5b743eab9de436de1a10f to your computer and use it in GitHub Desktop.
Save minhphong306/7554a54c9ca5b743eab9de436de1a10f to your computer and use it in GitHub Desktop.
Gọi hàng 2.go
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