問題解決方法:
在這個程序中,我們將使用 log.Panic() 函數在控製台屏幕上打印帶有時間戳的指定消息。 log.Panic() 類似於 log.Print() 函數,然後調用 Panic() 函數。
程序/源代碼:
下麵給出了演示 log.Panic() 函數的源代碼。給定的程序在 ubuntu 18.04 操作係統上編譯和執行成功。
// Golang program to demonstrate
// the log.Panic() function
package main
// Import log package to use
// Panic() function
import "log"
// Import fmt package to use Println() function
// to print the message on the console screen
import "fmt"
func main() {
log.Panic("Warning:logging data")
fmt.Println("Program finished")
}
輸出:
2021/04/22 14:00:04 Warning:logging data panic:Warning:logging data goroutine 1 [running]: log.Panic(0xc00002c768, 0x1, 0x1) /usr/local/go/src/log/log.go:333 +0xac main.main() /home/main.go:15 +0x61 Program exited:status 2.
說明:
在上麵的程序中,我們聲明了包 main。 main 包用於告訴 Go 語言編譯器必須編譯該包並生成可執行文件。在這裏,我們導入了 "fmt" 包以使用 Println() 函數,我們還導入了 "log" 包以使用 log.Panic() 函數。
在 main() 函數中,我們使用 log.Panic() 函數在控製台屏幕上打印帶有時間戳的指定消息。在這裏,我們在控製台屏幕上打印了一條警告消息。
log.Panic() 類似於 log.Print() 函數,然後調用 Panic() 函數。
log.Panic() 函數調用 Panic() 函數,這就是為什麽在上麵的程序中 fmt.Println() 函數沒有在 log.Panic() 函數之後調用。
相關用法
- Golang log.Panicf()用法及代碼示例
- Golang log.Print()用法及代碼示例
- Golang log.Printf()用法及代碼示例
- Golang log.Fatal()用法及代碼示例
- Golang log.Fatalf()用法及代碼示例
- Golang log.SetPrefix()用法及代碼示例
- Golang log.SetOutput()用法及代碼示例
- Golang log.SetFlags()用法及代碼示例
- Golang strconv.QuoteToASCII()用法及代碼示例
- Golang io.CopyBuffer()用法及代碼示例
- Golang reflect.Kind()用法及代碼示例
- Golang time.Parse()用法及代碼示例
- Golang bits.RotateLeft16()用法及代碼示例
- Golang math.Ceil()用法及代碼示例
- Golang atomic.AddInt32()用法及代碼示例
- Golang strings.ToLowerSpecial()用法及代碼示例
- Golang time.Time.MarshalJSON()用法及代碼示例
- Golang io.Pipe()用法及代碼示例
- Golang strings.EqualFold()用法及代碼示例
- Golang reflect.FuncOf()用法及代碼示例
注:本文由純淨天空篩選整理自 Golang program to demonstrate the log.Panic() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。