Error Handling in Go

Go 原生的 Error Type 先認識 go 的 error interface 1 2 3 type error interface { Error() string } 只要 struct 實作 Error() 方法,就會是 error type,例如 os package 裡面的 PathError https://golang.org/pkg/os/#PathError 1 2 3 4 5 6 7 8 // PathError records an error and the operation and file path that caused it. type PathError struct { Op string Path string Err error } func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() } 建立自己程式裡的 Error struct 依照使用情境 error strcut ...

26 July 2018 · 1 min · nyo