Skip to content

Instantly share code, notes, and snippets.

@minhphong306
Created October 23, 2021 10:48
Show Gist options
  • Save minhphong306/9b024eda59d09ada043fa0a6b1ca1497 to your computer and use it in GitHub Desktop.
Save minhphong306/9b024eda59d09ada043fa0a6b1ca1497 to your computer and use it in GitHub Desktop.
Gọi hàng.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
}
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