package main
import "fmt"
type Error struct {
Code int
Msg string
UserMsg string
}
func setErrorMsg(err *Error, value interface{}) {
err.Msg = fmt.Sprintf(err.Msg, value)
}
func main() {
e := Error{
Code: 400403,
Msg: "error: %v",
UserMsg: "錯誤",
}
setErrorMsg(&e, "hello world")
fmt.Println(e)
// 斷結(jié)構(gòu)體對象 e 是 結(jié)構(gòu)體Error的實(shí)例
_, ok1 := interface{}(e).(Error)
fmt.Println("ok1: ",ok1) // 輸出OK
}
輸出結(jié)果如下:
{400403 error: hello world 錯誤}
ok1: true
用對象進(jìn)行斷言時,需要先將對象轉(zhuǎn)換成空接口類型:
interface{}(e)