golang-panic格式化打印

可以用fmt.Panicf

// Panicf is equivalent to Printf() followed by a call to panic().
func Panicf(format string, v ...any) {

例子:

log.Panicf("Hello, %d\n", 2333)

也可以用fmt.Sprintf格式化输出到字符串,再用panic输出:

panic(fmt.Sprintf("cur = %d, filled = %d\n", cur, filled))

也可以用log.Fatalf

// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func Fatalf(format string, v ...any) {

例子:

log.Fatalf("cur = %d, filled = %d\n", cur, filled)

注意log.Fatalf不会打印trace,只会打印错误信息然后退出:https://stackoverflow.com/questions/24809287/how-do-you-get-a-golang-program-to-print-the-line-number-of-the-error-it-just-ca

log.Paniclog.Fatallnlog.Fatalpanic一样没有自带格式化打印。

posted @ 2024-09-28 14:14  寻找繁星  阅读(9)  评论(0编辑  收藏  举报