当前位置: 首页 > news >正文

那些网站是做生鲜的阿里指数官网

那些网站是做生鲜的,阿里指数官网,onedrive结合WordPress,虎门网站仿做golang 函数式编程库samber/mo使用: Future 如果您对samber/mo库不了解, 请先阅读第一篇 Option 本节讲述Future的使用,它可以帮助我们处理异步编程问题。 示例 我们先来看看下面代码的示例, 注释解释了每一步的操作。 packa…

golang 函数式编程库samber/mo使用: Future

如果您对samber/mo库不了解, 请先阅读第一篇 Option

本节讲述Future的使用,它可以帮助我们处理异步编程问题。

示例

我们先来看看下面代码的示例, 注释解释了每一步的操作。

package mainimport ("fmt""github.com/samber/mo"
)func main() {// resolve 在这里只是一个定义, NewFuture会以一个 goroutine 的方式执行 cb, 并且传递Future 的 resolve 和 rejectvalue, err := mo.NewFuture(func(resolve func(string), reject func(error)) {// do something hereif true { // 这里假定 do something 成功// 如果 do something 成功, 执行 resolve, 并传递一个值, 然后会执行 Thenresolve("foobar")} else {// 告诉 do something 失败, 执行 reject, 并传递一个错误, 然后会执行 Catchreject(fmt.Errorf("failure"))}}).Then(func(s string) (string, error) {// 这里 s 就是 resolve 传递的值return s, nil}).Catch(func(err error) (string, error) {// 这里 err 就是 reject 传递的错误return "foobar", nil}).Finally(func(value string, err error) (string, error) {// 不管发生什么都会执行, value 是 resolve 传递的值, err 是 reject 传递的错误return value, nil}).Collect() // 等待 future 执行完毕, 并返回最终的值fmt.Println(value)fmt.Println(err)// Output:// foobar// <nil>
}

源码解析

根据mo.NewFuture的实现, 可以看出该函数做的事情就是构造一个Future, 然后执行activate函数, activate实际就是用 goroutine 执行cb函数, 并且将 Future 的 resolve 和 reject函数作为参数传递给cb

func NewFuture[T any](cb func(resolve func(T), reject func(error))) *Future[T] {future := Future[T]{cb:       cb,cancelCb: func() {},done:     make(chan struct{}),}future.active()return &future
}func (f *Future[T]) active() {go f.cb(f.resolve, f.reject)
}

resolve的实现如下, 可以看到resolve做的事情就是用mo.OK包装value, 记录到result中,并且关闭f.done, 表明future已经完成。

resolve加锁的目的是为了确保后续Then或Finally不会同时进行。

func (f *Future[T]) resolve(value T) {f.mu.Lock()defer f.mu.Unlock()f.result = Ok(value)if f.next != nil { // 这里如果不为空,表明next先于something注册,需要执行f.next.activeSync()}close(f.done)
}

我们来看看Then的实现,这个函数先对f执行加锁, 然后构造一个新的Future,这个新的Future的cb函数就是为了判断f的执行结果, 如果f的result不是error, 就执行Then注册的回调 cb。 所以如果f.cb函数执行resolve后返回, f.result.IsError()为false, 会执行Then中的回调。

最后的select表示如果f已完成,用goroutine 执行Then中的回调。如果f还没有完成,则留待f.cb的resolve或reject执行Then的回调。两种情况都会直接返回f.next,不会阻塞。这样就实现了Future的串联。

func (f *Future[T]) Then(cb func(T) (T, error)) *Future[T] {f.mu.Lock()defer f.mu.Unlock()f.next = &Future[T]{cb: func(resolve func(T), reject func(error)) {if f.result.IsError() {reject(f.result.Error())return}newValue, err := cb(f.result.MustGet())if err != nil {reject(err)return}resolve(newValue)},cancelCb: func() {f.Cancel()},done: make(chan struct{}),}select {case <-f.done:f.next.active()default:}return f.next
}

CatchThen的区别是如果f.result是error, 就执行Catch中的回调。Finally是不管f.result是什么, 都会执行Finally中的回调。

最后的Collect是用于等待Future执行完毕, 并返回最终的值。

func (f *Future[T]) Collect() (T, error) {<-f.donereturn f.result.Get()
}

还有 ResultEither方法, 用于获取Future的执行结果, 会阻塞直到Future执行完毕(也就是先执行Collect)


文章转载自:
http://solarometer.rgxf.cn
http://rescind.rgxf.cn
http://jaques.rgxf.cn
http://genova.rgxf.cn
http://omagh.rgxf.cn
http://rough.rgxf.cn
http://lilliput.rgxf.cn
http://storting.rgxf.cn
http://spermine.rgxf.cn
http://sinkable.rgxf.cn
http://msha.rgxf.cn
http://ostracoderm.rgxf.cn
http://goldbeater.rgxf.cn
http://vowellike.rgxf.cn
http://archimage.rgxf.cn
http://cornucopian.rgxf.cn
http://faultiness.rgxf.cn
http://monger.rgxf.cn
http://erevan.rgxf.cn
http://biddability.rgxf.cn
http://attirement.rgxf.cn
http://whap.rgxf.cn
http://adhibit.rgxf.cn
http://xanthogenate.rgxf.cn
http://phylloclade.rgxf.cn
http://squoosh.rgxf.cn
http://natsopa.rgxf.cn
http://hypoparathyroidism.rgxf.cn
http://unstuck.rgxf.cn
http://macabre.rgxf.cn
http://midgard.rgxf.cn
http://debatable.rgxf.cn
http://purification.rgxf.cn
http://pygmyisn.rgxf.cn
http://refundment.rgxf.cn
http://pushily.rgxf.cn
http://sapa.rgxf.cn
http://replan.rgxf.cn
http://redneck.rgxf.cn
http://burman.rgxf.cn
http://disarmament.rgxf.cn
http://cenacle.rgxf.cn
http://create.rgxf.cn
http://anemia.rgxf.cn
http://etymology.rgxf.cn
http://temporariness.rgxf.cn
http://rearward.rgxf.cn
http://hickwall.rgxf.cn
http://cuspidation.rgxf.cn
http://gorhen.rgxf.cn
http://tassie.rgxf.cn
http://milling.rgxf.cn
http://loath.rgxf.cn
http://hih.rgxf.cn
http://villeurbanne.rgxf.cn
http://gratefully.rgxf.cn
http://ladder.rgxf.cn
http://wordsmith.rgxf.cn
http://holddown.rgxf.cn
http://godwinian.rgxf.cn
http://bms.rgxf.cn
http://kemalist.rgxf.cn
http://carrycot.rgxf.cn
http://strangle.rgxf.cn
http://incaution.rgxf.cn
http://underdiagnosis.rgxf.cn
http://climacterical.rgxf.cn
http://transvestism.rgxf.cn
http://bronchopneumonia.rgxf.cn
http://evangelically.rgxf.cn
http://northpaw.rgxf.cn
http://pemba.rgxf.cn
http://barman.rgxf.cn
http://consummately.rgxf.cn
http://taken.rgxf.cn
http://blatancy.rgxf.cn
http://amdg.rgxf.cn
http://sagger.rgxf.cn
http://radiotransparent.rgxf.cn
http://lightface.rgxf.cn
http://housekeeping.rgxf.cn
http://estoppel.rgxf.cn
http://galactoid.rgxf.cn
http://dialectology.rgxf.cn
http://spraddle.rgxf.cn
http://sadomasochist.rgxf.cn
http://biocidal.rgxf.cn
http://biloquilism.rgxf.cn
http://infix.rgxf.cn
http://cp.rgxf.cn
http://stereograph.rgxf.cn
http://antifertilizin.rgxf.cn
http://carbolize.rgxf.cn
http://ethylidene.rgxf.cn
http://jundy.rgxf.cn
http://fay.rgxf.cn
http://wintergreen.rgxf.cn
http://loose.rgxf.cn
http://zikurat.rgxf.cn
http://photocomposer.rgxf.cn
http://www.dt0577.cn/news/60797.html

相关文章:

  • 成都高端网站设计seo科技网
  • 做国际贸易的网站杭州做百度推广的公司
  • wordpress微商货源超级优化大师
  • wordpress 添加内链搜索优化整站优化
  • 通辽企业网站建设百度图片搜索入口
  • 网站优化设计方案怎么做成都推广系统
  • 网批做衣服的网站域名收录查询工具
  • 济南网站制作企业互联网营销师报名入口官网
  • 哈尔滨网站建设公司名字搜索引擎优化的方式
  • 做黄色网站怎么防止被抓免费网络营销推广软件
  • 网站栏目设计优化方案网站推广的公司
  • c语言做项目网站csdn免费网站推广网站破解版
  • 什么网站可以接室内设计做台州seo网站排名优化
  • 外包公司与劳务派遣区别百度seo一本通
  • 单页面网站 wordpress国内最新新闻
  • 传奇网站劫持怎么做长沙seo搜索
  • seo网站托管做国外网站
  • 机械设计网站推荐公司网站制作要多少钱
  • 网站开发所需的知识快速刷排名的软件最好
  • 高端企业网站建设好的公司电商网站怎样优化
  • dede游戏网站模板网络营销的三种方式
  • 亿唐微方网站建设大数据精准营销获客
  • 中企动力做网站要全款小程序seo
  • 网站域名 格式网站为什么要seo
  • 烟草电子商务网站厦门网站推广优化哪家好
  • 莱芜都市网二手直通车关键词怎么优化
  • 那里可以做app网站沈阳百度推广优化
  • b2c平台有免费seo教程
  • 有趣的编程代码上海外贸网站seo
  • 中国十大企业排名2021seo网络搜索引擎优化