打印错误对象中的堆栈信息
通过 fmt/glog
或者其他包打印错误对象时,默认情况下不会输出错误堆栈信息。例如:
package main
import (
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
)
func main() {
_, err := gjson.Encode(func() {})
fmt.Printf("err: %v", err)
}
执行后,终 端输出:
err: json.Marshal failed: json: unsupported type: func()
打印错误对象中的错误堆栈信息,应当使用 %+v
的格式化方式。例如:
package main
import (
"fmt"
"github.com/gogf/gf/v2/encoding/gjson"
)
func main() {
_, err := gjson.Encode(func() {})
fmt.Printf("err: %+v", err)
}
执行后,终端输出:
err: json.Marshal failed: json: unsupported type: func()
1. json.Marshal failed
1). github.com/gogf/gf/v2/internal/json.Marshal
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/internal/json/json.go:30
2). github.com/gogf/gf/v2/encoding/gjson.Encode
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/encoding/gjson/gjson_stdlib_json_util.go:41
3). main.main
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.test/main.go:10
2. json: unsupported type: func()