GO語言"fmt"包中"Errorf"函數的用法及代碼示例。
用法:
func Errorf(format string, a ...any) error
Errorf 根據格式說明符進行格式化,並將字符串作為滿足錯誤的值返回。
如果格式說明符包含帶有錯誤操作數的 %w 動詞,則返回的錯誤將實現返回操作數的 Unwrap 方法。包含多個 %w 動詞或為其提供不實現錯誤接口的操作數是無效的。 %w 動詞是 %v 的同義詞。
例子:
Errorf 函數允許我們使用格式化函數來創建說明性錯誤消息。
package main
import (
"fmt"
)
func main() {
const name, id = "bueller", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
fmt.Println(err.Error())
}
輸出:
user "bueller" (id 17) not found
相關用法
- GO ErrReader用法及代碼示例
- GO Encode用法及代碼示例
- GO EncodeToString用法及代碼示例
- GO EscapeString用法及代碼示例
- GO Encoder用法及代碼示例
- GO Expm1用法及代碼示例
- GO Encoding.Decode用法及代碼示例
- GO Exp2用法及代碼示例
- GO Expand用法及代碼示例
- GO Encoding.EncodeToString用法及代碼示例
- GO Equal用法及代碼示例
- GO Encoding.DecodeString用法及代碼示例
- GO Exp用法及代碼示例
- GO Ext用法及代碼示例
- GO Encoding.Encode用法及代碼示例
- GO ExpandEnv用法及代碼示例
- GO EqualFold用法及代碼示例
- GO EncodeRune用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Errorf。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。