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

去套路和延期

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 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含