当前位置:网站首页>Go closure usage example
Go closure usage example
2022-06-25 14:55:00 【Morning snail】
stay Go In language , Anonymous functions are needless to say , But closures need to be mentioned .
Closure : Capture external variables , It doesn't matter if these captured variables or constants are out of scope , As long as closures are in use , These variables will always exist .
Use closures to achieve division 0 Calculation of the square of natural numbers other than , Each call is required to return the square of the number of calls . The code is as follows :
package main
import "fmt"
func main() {
f := demo()
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
}
func demo() func() int {
var x int
return func() int {
x++
return x * x
}
}
Output results :
1
4
9
16
边栏推荐
- Daily question, Caesar code,
- Thymeleaf Usage Summary
- Gif动图如何裁剪?收下这个图片在线裁剪工具
- Two common ways for orcale to clear table data
- Function of getinstance() method
- Vs2019 scanf error
- QT file reading -qfile
- dev/mapper的解释
- NBD Network Block Device
- Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
猜你喜欢

Does stream even have application advanced learning? As a programmer, you know what

New good friend Pinia, leading the new era of state management

One question per day,

What moment makes you think there is a bug in the world?

ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析

Haven't you understood the microservice data architecture transaction management +acid+ consistency +cap+base theory? After reading it, you can completely solve your doubts

Flexible layout (display:flex;) Attribute details

Std:: vector minutes

JS component

关于win10 版本kicad 卡死的问题, 版本6.x
随机推荐
Kubernetes 理解kubectl/调试
One question per day, a classic simulation question
[untitled] PTA check password
【HBZ分享】LockSupport的使用
【Try to Hack】vulnhub DC1
Uniapp icon configuration
Extend JS copy content to clipboard
ffmpeg protocol concat 进行ts流合并视频的时间戳计算及其音画同步方式一点浅析
从408改考自主命题,211贵州大学考研改考
JS floating point multiplication and division method can not accurately calculate the problem
[Ocean University of China] Data Sharing for retest of initial Examination
Differences between member variables and local variables
【中国海洋大学】考研初试复试资料分享
What moment makes you think there is a bug in the world?
Open a restaurant
[untitled]
网上股票开户安不安全?有谁知道呢
JGG | overview of duhuilong group of Hebei University Research on plant pan genomics
basic_ String mind map
Source code analysis of synergetics and ntyco