基本示例:

package main
//定义错误
//error 也是个接口
import (
"errors"
"fmt"
) var errNotFound error = errors.New("Not found error") func main() {
fmt.Printf("error:%v", errNotFound)
}

错误处理:

package main

import (
"fmt"
"os"
"time"
) type PathError struct {
path string
op string
createTime string
message string
} func (p *PathError) Error() string {
return fmt.Sprintf("path=%s op=%s createTime=%s message=%s",
p.path, p.op, p.createTime, p.message)
}
func Open(filename string) error {
file, err := os.Open(filename)
if err != nil {
return &PathError{
path: filename,
op: "read",
message: err.Error(),
createTime: fmt.Sprintf("%v", time.Now()),
}
}
defer file.Close()
return nil
}
func main() {
err := Open("D:/project/src/go_dev/day7/example10/123.txt")
switch v := err.(type) {
case *PathError:
fmt.Println("get path error,", v)
default: }
}

错误处理

练习

package main

import "fmt"

func badCall() {
panic("bad end")
}
func test() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("Panicking%s\r\n", e)
}
}()
badCall()
fmt.Printf("After bad call\r\n")
}
func main() {
fmt.Printf("Calling test\r\n")
test()
}

Golang之定义错误(errors)的更多相关文章

  1. Flask(2)- 装饰器的坑及解决办法、flask中的路由/实例化配置/对象配置/蓝图/特殊装饰器(中间件、重定义错误页面)

    一.装饰器的坑以及解决方法 1.使用装饰器装饰两个视图函数,代码如下 from flask import Flask, redirect, render_template, request, sess ...

  2. jQuery Validate 表单验证插件----利用jquery.metadata.js将校验规则直接写在class属性里面并定义错误信息的提示

    一.下载依赖包 网盘下载:https://yunpan.cn/cryvgGGAQ3DSW  访问密码 f224 二. 添加一个另外一个插件jquery.metadata.js 并把校验规则写在控件里面 ...

  3. IE8下提示'console'没有定义错误

    在开发的过程中因为调试的原因,在代码中增加console.info("xxxx"),而未进行删除 在IE8下測试该代码所在的页面报错,例如以下: 须要注意的是,使用console对 ...

  4. MVC验证06-自定义错误信息

    原文:MVC验证06-自定义错误信息 本文体验自定义错误信息.   系统默认的错误信息 在"MVC验证02-自定义验证规则.邮件验证"中,我们自定义了一个验证Email的类.如果输 ...

  5. 编译器重复定义错误:error C2371: 'SIZE' : redefinition; different basic types

    我们常常会定义自己工程用的数据类型,可能会与Windows的基本数据类型冲突. vs会报重复定义错误:error C2371: 'SIZE' : redefinition; different bas ...

  6. Springboot学习05-自定义错误页面完整分析

    Springboot学习06-自定义错误页面完整分析 前言 接着上一篇博客,继续分析Springboot错误页面问题 正文 1-自定义浏览器错误页面(只要将自己的错误页面放在指定的路径下即可) 1-1 ...

  7. LNK2005错误——重复定义错误

    编程中经常能遇到LNK2005错误——重复定义错误,其实LNK2005错误并不是一个很难解决的错误.弄清楚它形成的原因,就可以轻松解决它了. 造成LNK2005错误主要有以下几种情况: 1.重复定义全 ...

  8. golang自己定义数据类型查询与插入postgresql中point数据

    golang自己定义数据类型查询与插入postgresql中point数据 详细代码例如以下: package main import ( "bytes" "databa ...

  9. 隐蔽的bean没有定义错误:No bean named 'SysJdTypeServiceImpl' is defined

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'SysJdTypeServiceImpl ...

随机推荐

  1. 使用minGW/cygwin在Windows是用于gcc开发

    刚才记录了下用eclipse在linux下开发,突然想起来也另一种方法:MinGW. MinGW是Windows的gcc开发工具,直接使用Windows的运行库,所以可以在windows下面方便的用g ...

  2. Android:手把手教你打造可缩放移动的ImageView(下)

    在上一篇Android:手把手教你打造可缩放移动的ImageView最后提出了一个注意点:当自定义的MatrixImageView如ViewPager.ListView等带有滑动效果的ViewGrou ...

  3. uva-10098

    所有的排列,但是要不重复 #include<stdio.h> #include<iostream> #include<sstream> #include<qu ...

  4. Delphi Berlin 窗体代码分离风格 回到Delphi7传统风格

    回到Delphi7传统风格 Floating Form Designer You can now detach the form designer in both, VCL and FireMonke ...

  5. gevent 实现io自动切换,gevent.join([]), gevent.spawn, 爬虫多并发的实现

    gevent 是一个第三方库,可以很容易的实现遇到io(文件传输)操作时,程序自动跳转到下一个程序 例一: 用gevent.sleep()  来模拟io操作 import gevent def foo ...

  6. ABAP-长文本处理

  7. Python之关系字段

    参考:https://blog.csdn.net/pugongying1988/article/details/72870264 关系字段:一对一,多对一,多对多 一对一:  现在有很多一对一辅导班, ...

  8. 转载:MySQL和Redis 数据同步解决方案整理

    from: http://blog.csdn.net/langzi7758521/article/details/52611910 最近在做一个Redis箱格信息数据同步到数据库Mysql的功能. 自 ...

  9. Supervisor 进程管理工具

    简介: Supervisor 进程管理工具 一.安装 shell > yum -y install python-pip shell > pip install supervisor # ...

  10. ng2-file-upload 使用记录

    最近这两周一直在修bug,修的很是痛苦,不过痛苦也是件好事,不然每天都是在做同样的事情,没有什么挑战,工作多无聊呀! 是吧. 大致说一下背景吧: 这个项目是两年前开新项目,到现在一直还在开发中,一直不 ...