douruobokui58233 2018-08-03 12:19 采纳率: 0%
浏览 7
已采纳

去套路和延期

func main() {
    defer fmt.Println("Main defer")
    go t1()
    go t2()
    go t3()
    time.Sleep(20 * time.Second)

}

func t1() {
    defer fmt.Println("t1 defer")
    time.Sleep(20 * time.Second)
}
func t2() {
    defer fmt.Println("t2 defer")
    time.Sleep(5 * time.Second)
    panic(New("T2"))
}
func t3() {
    defer fmt.Println("t3 defer")
    time.Sleep(20 * time.Second)
}

1 thread (t2) calls panic, t2 defer gets called. when t2 panics, every other thread is also terminated. I want every thread's defer to be called. It's a scenario where panic is must, panic is in one thread.. So I want every thread to be aware that program is going to exit. Any approach I can achieve that?

present output:

t2 defer
panic: T2

goroutine 19 [running]:
main.t2()
        C:/Users/Talha.Irfan/OneDrive - Bentley Systems, Inc/Desktop/go_test/src/main2/main.go:34 +0x105
created by main.main
        C:/Users/Talha.Irfan/OneDrive - Bentley Systems, Inc/Desktop/go_test/src/main2/main.go:21 +0xb0
  • 写回答

1条回答 默认 最新

  • dongmai6666 2018-08-03 13:00
    关注

    Nothing will be called after code panics. That's how panic works, your program will exists after panic. You can use channels to send the values to other go routines if panic occurs. One more thing, It is better to use Wait Groups to wait for all go routines to finish rather using time.Sleep.

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
        var wg sync.WaitGroup
        defer fmt.Println("Main defer")
        ch := make(chan int)
        wg.Add(1)
        go t1(ch, &wg)
        wg.Add(1)
        go t2(ch, &wg)
        wg.Add(1)
        go t3(ch, &wg)
        ch <- 1
        close(ch)
        wg.Wait()
    }
    
    func t1(ch chan int, wg *sync.WaitGroup) {
        defer fmt.Println("t1 defer")
        defer wg.Done()
    }
    
    func t2(ch chan int,wg *sync.WaitGroup) {
        defer fmt.Println("t2 defer")
        for {
             foo, ok := <- ch
             if !ok {
                    println("done")
                    wg.Done()
                    return
             }
             println(foo)
        }
    }
    func t3(ch chan int, wg *sync.WaitGroup) {
        defer fmt.Println("t3 defer")
        defer wg.Done()
    }
    

    Playground Example

    In case you are getting an error inside the code you can use recover to catch the error. This will led your code to run all goroutines.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        defer fmt.Println("Main defer")
        go t1()
        go t2()
        go t3()
        time.Sleep(2 * time.Second)
    
    }
    
    func t1() {
        defer fmt.Println("t1 defer")
    }
    func t2() {
        defer fmt.Println("t2 defer")
            defer func() {
                if r := recover(); r != nil {
                    println("panic:" + r.(string))
                }
            }()
    }
    func t3() {
        defer fmt.Println("t3 defer")
    }
    

    Working Code on Go playground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP