可以用fmt.Panicf
:
// Panicf is equivalent to Printf() followed by a call to panic().
func Panicf(format string, v ...any) {
例子:
.Panicf("Hello, %d\n", 2333) log
也可以用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) {
例子:
.Fatalf("cur = %d, filled = %d\n", cur, filled) log
注意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.Panic
、log.Fatalln
和log.Fatal
跟panic
一样没有自带格式化打印。