当前位置:网站首页>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
边栏推荐
- Study notes of cmake
- 移除区间(贪心)
- Power automatic test system nsat-8000, accurate, high-speed and reliable power test equipment
- It's not easy to understand the data consistency of the microservice architecture for the first time after six years as a programmer
- 网上股票开户安不安全?有谁知道呢
- 现在股票开户用什么app最安全?知道的给说一下吧
- Qmake uses toplevel or topbuilddir
- QT opens the print dialog box in a text editor
- Std:: vector minutes
- Judging the number of leap years from 1 to N years
猜你喜欢

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

从0到1完全掌握 XSS

Review of arrays and pointers triggered by a topic

JS recursion and while

JS component

SPARQL learning notes of query, an rrdf query language

Uniapp icon configuration

‘make_ unique’ is not a member of ‘std’

Judging the number of leap years from 1 to N years

Using Sphinx to automatically generate API documents from py source files
随机推荐
QQ情话糖果情话内容获取并保存
Brain tree (I)
Character encoding minutes
One question per day, a classic simulation question
Build a minimalist gb28181 gatekeeper and gateway server, establish AI reasoning and 3D service scenarios, and then open source code (I)
Review of arrays and pointers triggered by a topic
What is the safest app for stock account opening? Tell me what you know
New good friend Pinia, leading the new era of state management
Mutationobserver listens for DOM changes
Time stamp calculation and audio-visual synchronization of TS stream combined video by ffmpeg protocol concat
QT loading third-party library basic operation
[untitled]
14 -- validate palindrome string II
Thymeleaf Usage Summary
Automatic correlation between QT signal and slot
Daily question, magic square simulation
Yolov3 spp Darknet version to caffemodel and then to OM model
SPARQL learning notes of query, an rrdf query language
Custom instruction, mixing, routing, lifecycle
[deep learning] multi label learning