当前位置:网站首页>An interview question about recover in golang
An interview question about recover in golang
2022-07-25 20:53:00 【youngqqcn】
Write the following logic , Call every second proc And ensure that the program does not exit
package main
func main() {
go func() {
// 1 You need to write an algorithm here
// 2 Call every second proc function
// 3 Request that the program cannot exit
}()
select {
}
}
func proc() {
panic("ok")
}
Please code by yourself , Completion algorithm . The answer is at the end of the article
,
,
,
,
,
,
,
,
,
,
func main() {
go func() {
// 1 You need to write an algorithm here
// 2 Call every second proc function
// 3 Request that the program cannot exit
for {
t := time.NewTicker(time.Second)
select {
case <-t.C:
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("%s\n", r)
}
}()
proc()
}()
}
}
}()
select {
}
}
What is the problem with the following implementation :
func main() {
go func() {
// 1 You need to write an algorithm here
// 2 Call every second proc function
// 3 Request that the program cannot exit
defer func() {
if r := recover(); r != nil {
fmt.Printf("%s\n", r)
}
main()
}()
for {
t := time.NewTicker(time.Second)
select {
case <-t.C:
proc()
}
}
}()
select {
}
}
The stack space will be larger and larger , Know stack overflow , Program crash
In order to verify , Let's get rid of the timer
func main() {
go func() {
// 1 You need to write an algorithm here
// 2 Call every second proc function
// 3 Request that the program cannot exit
defer func() {
if r := recover(); r != nil {
// fmt.Printf("%s\n", r)
}
main()
}()
proc()
}()
select {
}
}
Run the program , Observe the memory space occupied by the process , It gets bigger and bigger , Less than a few seconds , The program was killed by the system
$ ./solution26
Have killed
边栏推荐
- [FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
- [FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes
- Cesium 多边形渐变色纹理(Canvas)
- 结构体,枚举类型与联合体
- 一道golang中关于map的并发读写的面试题
- Leetcode-6125: equal row and column pairs
- A detailed explanation of SCP command
- Leetcode customs clearance: hash table six, this is really a little simple
- wokerman 自定义写入日志文件
- leetcode-6129:全 0 子数组的数目
猜你喜欢

Cesium polygon gradient texture (canvas)

Unity vs -- the default debugging in VS is to start rather than attach to unity debugging

Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP

matlab----EEGLab查看脑电信号

Opencv learning Fourier transform experience and line direction Fourier transform code

103. (cesium chapter) cesium honeycomb diagram (square)

leetcode-6127:优质数对的数目
![[fiddlertx plug-in] use Fiddler to capture the package Tencent classroom video download (unable to capture the package solution)](/img/de/7a288ee8e6001235d4869c10503932.png)
[fiddlertx plug-in] use Fiddler to capture the package Tencent classroom video download (unable to capture the package solution)

程序的编译和运行

How to realize reliable transmission with UDP?
随机推荐
leetcode-6127:优质数对的数目
Qixin Jushi cloud spectrum new chapter | Haitai Fangyuan and Sichuan Unicom reach ecological strategic cooperation
matlab----EEGLab查看脑电信号
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input
The onnx model is exported as a TRT model
Embedded development: embedded foundation -- threads and tasks
wokerman 自定义写入日志文件
Based on pexels image material API, sort out the material resource library
Explain the principle of MySQL master-slave replication in detail
[cloud native] use of Nacos taskmanager task management
Huatai Securities account opening process, is it safe to open an account on your mobile phone
Card link
476-82(322、64、2、46、62、114)
Golang language quickly get started to comprehensive practical notes (go language, beego framework, high concurrency chat room, crawler)
Canvas fill gradient
Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
Unity vs -- the default debugging in VS is to start rather than attach to unity debugging
MPI学习笔记(二):矩阵相乘的两种实现方法
[MCU] 51 MCU burning those things