这个好:Go 1.20 将支持 Wrapping 多个 errors
  
        出品 | OSC开源社区(ID:    
    
        oschina2013)    
    
Go 近日接受了名为 「add support for wrapping multiple errors」 的提案。 该项提案对错误处理进行了优化,与 Go 1.13 为错误处理提供的新功能有关:Error Wrapping。引入 Error Wrapping 后,Go 同时为 详情查看    
    
        https://github.com/golang/go/issues/53435   
    
        。
        详情查看    
    
        https://github.com/golang/go/issues/53435   
    
        。   
我为大家整理了一份 从入门到进阶的Go学习资料礼包 ,包含学习建议:入门看什么,进阶看什么。 关注公众号 「polarisxu」,回复 ebook 获取;还可以回复「进群」,和数万 Gopher 交流学习。
Go 近日接受了名为 「add support for wrapping multiple errors」 的提案。 该项提案对错误处理进行了优化,与 Go 1.13 为错误处理提供的新功能有关:Error Wrapping。引入 Error Wrapping 后,Go 同时为
        
            errors      
    
    
        包添加了 3 个工具函数,分别是    
    
        
            Unwrap      
    
    
        、   
    
        
            Is      
    
    
        和   
    
        
            As      
    
    
        。   
        对于  
    
        「add support for wrapping multiple errors」  
    
        提案,顾名思义就是一个错误可以包裹多个错误。
      
        Unwrap() []error
      
    
  
  
        提出该提案的开发者表示,重用  
    
        
            Unwrap      
    
    
         避免了与现有 Unwrap 方法产生歧义,从     
    
        
            Unwrap      
    
    
         中返回一个长度为 0 的列表意味着错误没有包裹任何内容。调用方不得修改由   
    
        
            Unwrap      
    
    
         返回的列表, 
    
        
            Unwrap      
    
    
         返回的列表不得包含任何    
    
        
            nil     
    
    
         错误。    
        他还对     
    
        
            errors.Is       
    
    
         和  
    
        
            errors.As       
    
    
         函数进行了更新,实现对 multiple errors 进行     
    
        
            Unwrap      
    
    
         操作。    
        
            errors.Join     
    
    
         函数提供了 multierr 的简单实现:
      
        // Join returns an error that wraps the given errors.
      
      
        // Any nil error values are discarded.
      
      
        // The error formats as the text of the given errors, separated by newlines.
      
      
        // Join returns nil if errs contains no non-nil values.
      
      
        func Join(errs ...error) error
      
    
  
  
        目前该提案已被接受,作者表示将在 Go 1.20 中提供:   
   详情查看    
    
        https://github.com/golang/go/issues/53435   
    
        。
        详情查看    
    
        https://github.com/golang/go/issues/53435   
    
        。   
    推荐阅读
    
    
我为大家整理了一份 从入门到进阶的Go学习资料礼包 ,包含学习建议:入门看什么,进阶看什么。 关注公众号 「polarisxu」,回复 ebook 获取;还可以回复「进群」,和数万 Gopher 交流学习。
评论
