Small Example of Go language error handling and go language example

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.