'How to intercept `runtime.Goexit()` signal
package main
import (
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"runtime"
)
var (
Errors map[string]string
err error
)
func CheckErr(err error) {
if err != nil {
pc, file, line, _ := runtime.Caller(1)
color.New(color.FgRed).PrintfFunc()(" [app]")
color.New(color.FgMagenta).PrintfFunc()("%v:", time.Now().Format("15:04:05.00000"))
color.New(color.FgGreen).PrintfFunc()("%v] %v:%v:", path.Ext(runtime.FuncForPC(pc).Name())[1:], path.Base(file), line)
color.Yellow(" %v", err)
Errors["app "+time.Now().Format("15:04:05.00000")+" "+path.Ext(runtime.FuncForPC(pc).Name())[1:]+" "+path.Base(file)+":"+strconv.Itoa(line)] = err.Error()
runtime.Goexit()
}
}
func test(hello string) string{
// ...
if err!=nil{
CheckErr(err)
}
text := hello+" world"
return test
}
func main() {
req.POST("/hello", func(context *gin.Context) {
message:=test("hello")
context.JSON(200, gin.H{
"message": message,
"Errors": Errors,
})
}
}
- CheckErr is a function that collects error logs, prints errors and exits
- main is a gin service
- test is the data acquisition function of gin
- Cannot use
gocoroutine becausetestneeds to return data
How can I go back to
ginand return the error message whentestfails?
Solution 1:[1]
If you insist on using runtime.Goexit() then you have to use another goroutine. Together with chan or sync.WaitGroup it works just fine since goexit runs all deferred calls.
// Goexit runs all deferred calls before terminating the goroutine. Because Goexit
// is not a panic, any recover calls in those deferred functions will return nil.
func CheckErr(err error, done chan struct{}) {
defer func() { done <- struct{}{} }()
if err != nil {
// ...
runtime.Goexit()
}
}
func test(hello string) string {
// ...
if err != nil {
done := make(chan struct{})
go CheckErr(err, done)
<-done
}
text := hello + " world"
return text
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | medasx |
