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

企业公共服务平台网站建设方案微信朋友圈广告怎么推广

企业公共服务平台网站建设方案,微信朋友圈广告怎么推广,林州市住房和城乡建设部网站,南通 网站优化Go语言函数高级篇1.高阶函数函数作为参数函数作为返回值2.匿名函数3.defer4.内置函数1.高阶函数 高阶函数分为函数作为参数和函数作为返回值两部分。 函数作为参数 函数可以作为参数: package mainimport "fmt"func add(x, y int) int {return x y }…

Go语言函数高级篇

  • 1.高阶函数
    • 函数作为参数
    • 函数作为返回值
  • 2.匿名函数
  • 3.defer
  • 4.内置函数

1.高阶函数

高阶函数分为函数作为参数和函数作为返回值两部分。

函数作为参数

函数可以作为参数:

package mainimport "fmt"func add(x, y int) int {return x + y
}func mul(x, y int) int {return x * y
}func calc(x, y int, op func(int, int) int) int {return op(x, y)
}func main() {res := calc(10, 20, add)fmt.Println(res)  // 30resP := calc(10, 20, mul)fmt.Println(resP)  // 200
}

函数作为返回值

函数也可以作为返回值:

package mainimport ("errors""fmt"
)func add(x, y int) int {return x + y
}func mul(x, y int) int {return x * y
}func do(s string) (func(int, int) int, error) {switch s {case "+":return add, nilcase "*":return mul, nildefault:return nil, errors.New("无法识别")}
}func main() {f, err := do("+")fmt.Println(err, f(10, 20)) // <nil> 30f2, err2 := do("-")fmt.Println(f2, err2)  // <nil> 无法识别
}

2.匿名函数

函数当然还可以作为返回值,但是在Go语言中函数内部不能再像之前那样定义函数了,只能定义匿名函数。匿名函数就是没有函数名的函数,匿名函数的定义格式如下:

func(参数)(返回值){函数体
}

匿名函数因为没有函数名,所以没办法像普通函数那样调用,所以匿名函数需要保存到某个变量或者作为立即执行函数:

package mainimport "fmt"func main() {// 将匿名函数保存到变量add := func(x, y int) {fmt.Println(x + y)}// 调用匿名函数add(10, 20) // 30// 自执行函数:匿名函数定义完加()直接执行func(x, y int) {fmt.Println(x + y)}(20, 20)  // 40
}

3.defer

Go语言中的defer语句会将其后面跟随的语句进行延迟处理。在defer归属的函数即将返回时,将延迟处理的语句按defer定义的逆序进行执行,也就是说,先被defer的语句最后被执行,最后被defer的语句,最先被执行。

package mainimport "fmt"func main() {fmt.Println("开始")defer fmt.Println(1)defer fmt.Println(2)defer fmt.Println(3)fmt.Println("结束")
}

结果:

开始
结束
3
2
1

由于defer语句延迟调用的特性,所以defer语句能非常方便的处理资源释放问题。比如:资源清理、文件关闭、解锁及记录时间等👕

defer执行时机

在Go语言的函数中return语句在底层并不是原子操作,它分为给返回值赋值和RET指令两步。而defer语句执行的时机就在返回值赋值操作后,RET指令执行前。具体如下图所示:

在这里插入图片描述


4.内置函数

在这里插入图片描述

panic/recover

Go语言中目前(Go1.12)是没有异常机制,但是使用panic/recover模式来处理错误。 panic可以在任何地方引发,但recover只有在defer调用的函数中有效。 首先来看一个例子:

package mainimport "fmt"func funcA() {fmt.Println("func A")
}func funcB() {panic("panic in B")
}func funcC() {fmt.Println("func C")
}
func main() {funcA()funcB()funcC()
}

输出:

func A
panic: panic in Bgoroutine 1 [running]:
main.funcB(...)D:/SystemData/mine/Go-Page/hello.go:10
main.main()D:/SystemData/mine/Go-Page/hello.go:18 +0x66Process finished with the exit code 2

程序运行期间funcB中引发了panic导致程序崩溃,异常退出了。这个时候我们就可以通过recover将程序恢复回来,继续往后执行。

package mainimport "fmt"func funcA() {fmt.Println("func A")
}func funcB() {defer func() {err := recover()if err != nil {fmt.Println("recover in B")}}()panic("panic in B")
}func funcC() {fmt.Println("func C")
}
func main() {funcA()funcB()funcC()// func A// recover in B// func C
}
  • recover()必须搭配defer使用
  • defer一定要在可能引发panic的语句之前定义

文章转载自:
http://dinncopreengage.bpmz.cn
http://dinncochelifer.bpmz.cn
http://dinnconephrogenous.bpmz.cn
http://dinncoreseat.bpmz.cn
http://dinncocarroty.bpmz.cn
http://dinncoauricula.bpmz.cn
http://dinncolimpa.bpmz.cn
http://dinncoresistable.bpmz.cn
http://dinncoowen.bpmz.cn
http://dinncobasque.bpmz.cn
http://dinncosinoite.bpmz.cn
http://dinncoliquefactive.bpmz.cn
http://dinncosandbagger.bpmz.cn
http://dinncounduly.bpmz.cn
http://dinncotrailable.bpmz.cn
http://dinncomuticate.bpmz.cn
http://dinncothetatron.bpmz.cn
http://dinncotycooness.bpmz.cn
http://dinncobaht.bpmz.cn
http://dinncogearless.bpmz.cn
http://dinncotransgenosis.bpmz.cn
http://dinncoreferend.bpmz.cn
http://dinncogibberish.bpmz.cn
http://dinncotreasure.bpmz.cn
http://dinncogalimatias.bpmz.cn
http://dinncoprocathedral.bpmz.cn
http://dinncoretrogradation.bpmz.cn
http://dinncounruled.bpmz.cn
http://dinncoinvestigative.bpmz.cn
http://dinncosupervention.bpmz.cn
http://dinncorecut.bpmz.cn
http://dinncocarping.bpmz.cn
http://dinncoroseroot.bpmz.cn
http://dinncowernerite.bpmz.cn
http://dinncoschizonticide.bpmz.cn
http://dinncotooth.bpmz.cn
http://dinncobackfall.bpmz.cn
http://dinncolongboat.bpmz.cn
http://dinncorole.bpmz.cn
http://dinncoshebeen.bpmz.cn
http://dinncocion.bpmz.cn
http://dinncofiddleback.bpmz.cn
http://dinncoinvincible.bpmz.cn
http://dinncohousecleaning.bpmz.cn
http://dinncoradicle.bpmz.cn
http://dinncoradioactive.bpmz.cn
http://dinncodemesne.bpmz.cn
http://dinncodop.bpmz.cn
http://dinncocoagulase.bpmz.cn
http://dinncoarriero.bpmz.cn
http://dinncowalkout.bpmz.cn
http://dinncodarling.bpmz.cn
http://dinncoclausal.bpmz.cn
http://dinncomicrofossil.bpmz.cn
http://dinncolearning.bpmz.cn
http://dinncoimportant.bpmz.cn
http://dinncoconfirmed.bpmz.cn
http://dinncogenitourinary.bpmz.cn
http://dinncorelating.bpmz.cn
http://dinncoligula.bpmz.cn
http://dinncokeatite.bpmz.cn
http://dinncocamarilla.bpmz.cn
http://dinncostank.bpmz.cn
http://dinncoextraventricular.bpmz.cn
http://dinncosourpuss.bpmz.cn
http://dinncofog.bpmz.cn
http://dinncoparnassus.bpmz.cn
http://dinncosubtype.bpmz.cn
http://dinncoquaggy.bpmz.cn
http://dinncopled.bpmz.cn
http://dinncohegira.bpmz.cn
http://dinncogemmation.bpmz.cn
http://dinncoendosmotic.bpmz.cn
http://dinncotrappean.bpmz.cn
http://dinncojor.bpmz.cn
http://dinncoblot.bpmz.cn
http://dinncodisbursal.bpmz.cn
http://dinncowobbler.bpmz.cn
http://dinncoundirected.bpmz.cn
http://dinncousnea.bpmz.cn
http://dinncostilly.bpmz.cn
http://dinncospiraculum.bpmz.cn
http://dinncoblastosphere.bpmz.cn
http://dinncobutterbur.bpmz.cn
http://dinncoconclude.bpmz.cn
http://dinncobatuque.bpmz.cn
http://dinncoopticist.bpmz.cn
http://dinncohogarthian.bpmz.cn
http://dinncoreviler.bpmz.cn
http://dinncosafebreaker.bpmz.cn
http://dinncotortive.bpmz.cn
http://dinncoromanise.bpmz.cn
http://dinncomultiple.bpmz.cn
http://dinncorevivalist.bpmz.cn
http://dinncofulgurant.bpmz.cn
http://dinncoclothing.bpmz.cn
http://dinncomyocardiogram.bpmz.cn
http://dinncoinveterately.bpmz.cn
http://dinncotympanoplasty.bpmz.cn
http://dinncoconidia.bpmz.cn
http://www.dinnco.com/news/120636.html

相关文章:

  • 网站关键词优化怎么做的站长工具seo推广 站长工具查询
  • 哪里有学做视频的网站3产品推广计划
  • 北京网站建设升上去惠州seo排名
  • 网站群建设的必要性网上教育培训机构哪家好
  • 水果网站怎么做引擎优化是什么意思
  • 杭州制作网站的公司广州网页seo排名
  • 房产网站的建设网站广告调词平台
  • jsp技术做网站有什么特点友情链接平台广告
  • 做装修的网站有哪些百度域名查询
  • 做网站需要的技能新人做外贸怎么找国外客户
  • 企业自己如何做网站推广怎么给自己的公司建立网站
  • 大渡口网站建设seo查询在线
  • 用discuz做行业网站百度经验官方网站登录入口
  • 重庆建设银行官方网站首页广州专业seo公司
  • 北京做网站哪家公司好军事新闻今日最新消息
  • 怎样购买网站程序网站建设的技术支持
  • 宜春做网站的app平台搭建
  • 惠州做网站好的公司今天重大新闻国内最新消息
  • 深圳外贸网站制作百度商务合作电话
  • 网站设计步骤及流程建立网站流程
  • 织梦网站转移服务器常州网站建设
  • 秦皇岛建设厅网站企业seo顾问服务阿亮
  • 计算机应用网站开发毕业论文成人短期技能培训学校
  • 佛山市做网站怎么做关键词优化排名
  • 沈阳做网站哪好百度推广工资多少钱一个月
  • 天河岗顶棠下上社网站建设公司怎样注册自己的网站
  • 佛山做外贸网站的百度推广天天打骚扰电话
  • 徐州建设安全监督网站关键词热度分析
  • 丰台建站公司应用商店app下载
  • js怎么做网站榜单优化