Small Example of Go language error handling and go language example
Full Screen error processing is a tragedy, and it is not conducive to distinguishing the error.
We recommend that you use custom errors in the project and then process them in a centralized manner.
Package main // error handling method demonstration // author: Xiong Chuan Liang // date: 2015-2-26import "fmt" import "errors" func main () {errType (test0 ()) errType (test1 ("test1") errType (test2 (500) errType (test3 ("test3") errType (test4 ("test4 "))} type Error1 struct {arg interrMsg string} func (e * Error1) Error () string {return fmt. sprintf ("% s", e. errMsg)} type Error2 struct {arg stringerrMsg string} func (e * Error2) Error () string {return fmt. sprintf ("% s", e. errMsg)} func test0 () error {return errors. new ("errors. new ()-test0 () ")} func test1 (arg string) error {return fmt. errorf ("fmt. errorf ()-test1 () ")} func test2 (arg int) * Error1 {return & Error1 {arg," Error1 {}-test2 () "} func test3 (arg string) error {return & Error2 {arg," Error2 {}-test3 () "} func test4 (arg string) * Error2 {return & Error2 {arg, "Error2 {}-test4 ()"} func errType (err interface {}) {switch e: = err. (type) {case * Error1: fmt. println ("Type: Error1", e) case * Error2: fmt. println ("Type: Error2", e) case error: fmt. println ("Type: error", e) default: fmt. println ("Type: default", e) }}/ * Running Effect: Type: error errors. new ()-test0 () Type: error fmt. errorf ()-test1 () Type: Error1 Error1 {}-test2 () Type: Error2 Error2 {}-test3 () Type: Error2 Error2 {}-test4 ()*/
MAIL: xcl_168@aliyun.com
BLOG: http://blog.csdn.net/xcl168