当前位置:网站首页>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
边栏推荐
- Automated testing ----- selenium (I)
- Canvas fill gradient
- Differences between seaslog and monolog log systems, installation steps of seaslog [easy to understand]
- Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
- Vulnhub | dc: 5 | [actual combat]
- Implementation of simple registration and login
- Basic knowledge of Marine Geology
- Leetcode skimming -- guess the size of numbers II 375 medium
- Compilation and operation of program
- Temperature and humidity environment monitoring system based on stm32
猜你喜欢

Opencv learning Fourier transform experience and line direction Fourier transform code

DDD的Go实战

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

LeetCode刷题——猜数字大小II#375#Medium

Fanoutexchange switch code tutorial
![[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes](/img/65/4dd3a521946e753c79d3db1fa0a4f4.png)
[FAQ] access the HMS core push service, and the server sends messages. Cause analysis and solutions of common error codes

Kubernetes进阶部分学习笔记

Step num problem
![[MCU] 51 MCU burning those things](/img/fa/8f11ef64a18114365c084fff7d39f6.png)
[MCU] 51 MCU burning those things

【FiddlerTX插件】使用Fiddler抓包腾讯课堂视频下载(抓不到包解决方案)
随机推荐
Basic knowledge of Marine Geology
Leetcode customs clearance: hash table six, this is really a little simple
matlab----EEGLab查看脑电信号
day04_ array
Decompile app
Online XML to JSON tool
MySQL master-slave replication data synchronization, summary of common problems
leetcode-6130:设计数字容器系统
ROS_ Rqt toolbox
Matlab---eeglab check EEG signal
Card link
leetcode-919:完全二叉树插入器
Niuke-top101-bm37
MPI学习笔记(二):矩阵相乘的两种实现方法
Opencv learning Fourier transform experience and line direction Fourier transform code
Principle analysis of bootloader
leetcode-146:LRU 缓存
What's special about Huawei's innovative solutions to consolidate the foundation of ERP for small and medium-sized enterprises?
The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
IEC61131 address representation