Go中的异常处理(基础)

Go 中异常处理

主要掌握 一下几个方面:

  1. 掌握error接口
  2. 掌握defer延迟
  3. 掌握panic及recover

error接口

error是指程序中出现不正常的情况,从而导致程序无法正常运行;

go中为错误的类型提供了简单的错误处理机制

go中error的源码:

// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
type error interface {
   
    
    
	Error() string
}

可以看到接口中定义了Error方法,我们可以实现这个方法已实现error接口;

猜你喜欢

转载自blog.csdn.net/weixin_54061333/article/details/130813700