# goroutine异常捕获

```go
package main

import (
    "log"
    "time"
)

//2021/03/01 00:22:02 start demo
//2021/03/01 00:22:02 recover goroutine err: this is a panic demo
//2021/03/01 00:22:05 end demo
func main() {
    log.Println("start demo")
    Go(PanicDemo)
    time.Sleep(3 * time.Second)
    log.Println("end demo")
}

func PanicDemo() {
    panic("this is a panic demo")
}

// 捕获异常的goroutine
func Go(x func()) {
    go func() {
        defer func() {
            if err := recover(); err != nil {
                log.Printf("recover goroutine err: %v", err)
            }
        }()

        // 执行goroutine
        x()
    }()
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://1005281342.gitbook.io/code-porter/yu-fa-ji-chu/yi-chang-chu-li/demo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
