当前位置:网站首页>一道golang中关于recover的面试题
一道golang中关于recover的面试题
2022-07-25 20:48:00 【youngqqcn】
写出以下逻辑,要求每秒钟调用一次proc并保证程序不退出
package main
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
}()
select {
}
}
func proc() {
panic("ok")
}
请自行编码,完成算法。答案在文末
,
,
,
,
,
,
,
,
,
,
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
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 {
}
}
以下实现有什么问题:
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
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 {
}
}
栈空间会越来越大,知道栈溢出,程序崩溃
为了验证,我们去掉定时器
func main() {
go func() {
// 1 在这里需要你写算法
// 2 要求每秒钟调用一次proc函数
// 3 要求程序不能退出
defer func() {
if r := recover(); r != nil {
// fmt.Printf("%s\n", r)
}
main()
}()
proc()
}()
select {
}
}
运行程序,观察进程占用的内存空间,会越来越大,不到几秒钟,程序被系统杀死
$ ./solution26
已杀死
边栏推荐
- 使用oap切面导致controller被重复调用
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- [noi simulation] string matching (suffix automata Sam, Mo team, block)
- Unity vs -- the default debugging in VS is to start rather than attach to unity debugging
- [technical dry goods] how to ensure the idempotency of the interface?
- [today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
- Scan delete folder problems
- Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
- Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
- MPI learning notes (II): two implementation methods of matrix multiplication
猜你喜欢
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]
![[paper reading] unpaired image to image translation using cycle consistent advantageous networks](/img/73/69651dd8ecfdddd1cae13a1d223d51.png)
[paper reading] unpaired image to image translation using cycle consistent advantageous networks

Introduction to several scenarios involving programming operation of Excel in SAP implementation project
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)

Online random coin tossing positive and negative statistical tool

476-82(322、64、2、46、62、114)

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!

Key network protocols in tcp/ip four layer model

Compilation and operation of program
随机推荐
seven point two three
Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)
LeetCode刷题——猜数字大小II#375#Medium
MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
Temperature and humidity environment monitoring system based on stm32
[matlab] download originality documents based on oil monkey script and MATLAB
[MSA] a brief description of the moveit Configuration Assistant chain in planning groups
leetcode-919:完全二叉树插入器
牛客-TOP101-BM37
2022.7.24-----leetcode.1184
[leetcode] 28. Implement strstr ()
wokerman 自定义写入日志文件
leetcode-114:二叉树展开为链表
In June 2021, the interview suffered a Waterloo. Is it so convoluted now
PayPal PHP product trial period "recommended collection"
"Shallow in and shallow out" MySQL and InnoDB [it is recommended to collect and share]
【FiddlerTX插件】使用Fiddler抓包腾讯课堂视频下载(抓不到包解决方案)
从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
[tensorrt] trtexec tool to engine
MySQL master-slave replication data synchronization, summary of common problems