[Go]訊息併發發送

小黑
發佈於後端
2024/01/10閱讀時間約 1 分鐘
package main

import "fmt"

func SendNotification(user string) chan string {

//......
//建立一個通道來保存訊息
notifications := make(chan string, 500)

//開啟一個通道
go func() {
//將訊息放入通道
notifications <- fmt.Sprintf("Hi %s, welcome to our site!", user)
}()

return notifications
}

func main() {
//獲取x的消息
x := SendNotification("xxx")
//獲取y的消息
y := SendNotification("yyy")

//獲取訊息的返回
fmt.Println(<-x)
fmt.Println(<-y)
}






7會員
78內容數
嗨,我是一名程式設計師,會在這分享開發與學習紀錄。
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容