5.21 Go秘籍:异步收割,永葆单身
Posted 目前在腾讯
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5.21 Go秘籍:异步收割,永葆单身相关的知识,希望对你有一定的参考价值。
[本文出自天外归云的博客园]
今天是5.21,是现代的年轻人喜爱过的节日,也是玫瑰花的葬礼。身为人生工程师,又有几人曾用代码谱写爱的史诗?
Keep something in my mind,异步永葆单身秘诀:
package main import ( "log" "testing" "github.com/stretchr/testify/assert" ) // Girl 女孩 type Girl struct { // Name 名字 Name string } // AcceptConfess 接收表白 func (girl *Girl) AcceptConfess(boy Boy) { log.Printf("%+v接收了%+v的表白", girl.Name, boy.Name) boy.LovePraiseChan <- LovePraise{From: girl.Name, Words: "同意"} } // RefuseConfess 拒绝表白 func (girl *Girl) RefuseConfess(boy Boy) { log.Printf("%+v拒绝了%+v的表白", girl.Name, boy.Name) boy.LovePraiseChan <- LovePraise{From: girl.Name, Words: "不同意"} } // Boy 男孩 type Boy struct { // Name 名字 Name string // Status 状态(单身或非单身) Status string // LoveChan 爱的表白通道 LovePraiseChan chan LovePraise } // LovePraise 爱的表白 type LovePraise struct { From string To string Words string } // WaitForLoveCallBack 等待爱的回复 func (me *Boy) WaitForLoveCallBack() LovePraise { // 永葆单身 for { select { case rec := <-me.LovePraiseChan: if rec.Words == "同意" { return LovePraise{ From: me.Name, To: rec.From, Words: "我群发的,有人先同意了,下次", } } else if rec.Words == "不同意" { return LovePraise{ From: me.Name, To: rec.From, Words: "我朋友拿我手机群发的", } } } } } // 测试爱的表达 func TestLovePraise(t *testing.T) { // 开始测试 lucy := Girl{"Lucy"} lily := Girl{"Lily"} girls := []Girl{lucy, lily} me := Boy{Name: "我", LovePraiseChan: make(chan LovePraise), Status: "单身"} for i := 0; i < len(girls); i++ { go func() { // 异步等待爱的回复——不耽误正常吃饭睡觉 callBack := me.WaitForLoveCallBack() log.Printf("%+v回复%+v道:%+v", callBack.From, callBack.To, callBack.Words) }() } lucy.AcceptConfess(me) lily.RefuseConfess(me) // 断言 assert.Equal(t, "单身", me.Status) }
运行结果:
以上是关于5.21 Go秘籍:异步收割,永葆单身的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础
解决go: go.mod file not found in current directory or any parent directory; see ‘go help modules‘(代码片段