为了理解go error,进一步学习github.com/pkg/errors作的训练。

http://www.shtml.net/article/content/tok/48369/id/37733.html

https://studygolang.com/articles/17430?fr=sidebar

https://blog.csdn.net/weixin_42506905/article/details/94482183

https://blog.csdn.net/fwhezfwhez/article/details/79175376

https://www.jianshu.com/p/75d3682cd135

package main

import (
	"fmt"
	"os"
	"runtime"

	"github.com/pkg/errors"
)

func main() {
	content, err := openFile()
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(string(content))
	}

	// github.com/pkg/errors测试
	errPkg := test1()
	if errPkg != nil {
		fmt.Println("err:", errPkg)
	}

	fmt.Printf("err :%+v\n", errPkg)

	fmt.Println("Hello world")
}

type stack []uintptr

type errorString struct {
	s string
	*stack
}

func callers() *stack {
	const depth = 32
	var pcs [depth]uintptr
	n := runtime.Callers(3, pcs[:])
	var st stack = pcs[0:n]
	return &st
}

func newError(text string) error {
	return &errorString{
		s:     text,
		stack: callers(),
	}
}

//error只有一个方法 Error,只要实现了这个方法,就是实现了error
func (e *errorString) Error() string {
	return e.s
}

//只是模拟一个错误
func openFile() ([]byte, error) {
	return nil, newError("new custom error")
}

func test1() error {
	_, err := os.Open("helloworld.txt")
	if err != nil {
		//return errors.WithMessage(err, "test1")
		return errors.Wrap(err, "test1")
	}
	return nil
}

  

github.com/pkg/errors库学习的更多相关文章

  1. GitHub学习二-将本地Git库与Github上的Git库相关联

    0 git clone 如果是从自己的github clone,一键搞定,和三步的git remote add功能一样. 1.创建本地库 新建目录,右键git bash here,输入 git ini ...

  2. [git 学习篇] 关联github和本地创库

    所以,github和本地创库是通过ssh传送,所以要将公钥拷贝到远程创库上(比如我ssh 远程服务器时,先将本地的公钥,拷贝到远程服务器的某个文件上(http://www.cnblogs.com/li ...

  3. 100个Github上Android开源库

    项目名称 项目简介 1. react-native 这个是 Facebook 在 React.js Conf 2015 大会上推出的基于 JavaScript 的开源框架 React Native, ...

  4. dlib库学习之一

    dlib库学习之一 1.介绍 跨平台 C++ 通用库 Dlib 发布 ,带来了一些新特性,包括概率 CKY 解析器,使用批量同步并行计算模型来创建应用的工具,新增两个聚合算法:中国低语 (Chines ...

  5. muduo网络库学习笔记(三)TimerQueue定时器队列

    目录 muduo网络库学习笔记(三)TimerQueue定时器队列 Linux中的时间函数 timerfd简单使用介绍 timerfd示例 muduo中对timerfd的封装 TimerQueue的结 ...

  6. github渗透测试工具库

    本文作者:Yunying 原文链接:https://www.cnblogs.com/BOHB-yunying/p/11856178.html 导航: 2.漏洞练习平台 WebGoat漏洞练习平台: h ...

  7. github渗透测试工具库[转载]

    前言 今天看到一个博客里有这个置顶的工具清单,但是发现这些都是很早以前就有文章发出来的,我爬下来后一直放在txt里吃土.这里一起放出来. 漏洞练习平台 WebGoat漏洞练习平台:https://gi ...

  8. python 操作exls学习之路1-openpyxl库学习

    这篇要讲到的就是如何利用Python与openpyxl结合来处理xlsx表格数据.Python处理表格的库有很多,这里的openpyxl就是其中之一,但是它是处理excel2007/2010的格式,也 ...

  9. Github实例教程-创建库、创建主页

    以README文件为实例,具体介绍github的使用过程 请先下载git,然后配置下面内容: ( 我的系统是debian,其它版本号的UNIX/Linux有区别),windows的临时不清楚. (一) ...

随机推荐

  1. Power BI 概念及 国内版Pro 试用账户注册流程

    视频内容: Power BI 基本概念:https://v.qq.com/x/page/s3026nn69eu.html Power BI Pro 世纪互联版本试用账号注册:https://v.qq. ...

  2. C# 集合汇总

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  3. Spring Boot 2.2.2.RELEASE 版本中文参考文档【3.1】

    使用Spring Boot 本节将详细介绍如何使用Spring Boot.它涵盖了诸如构建系统,自动配置以及如何运行应用程序之类的主题.我们还将介绍一些Spring Boot最佳实践.尽管Spring ...

  4. 设置UICollectionViewCell圆角和阴影

    设置cell圆角: cell.contentView.layer.cornerRadius =2.0f; cell.contentView.layer.borderWidth =1.0f; cell. ...

  5. PWA 学习笔记(五)

    离线与缓存 资源请求的拦截代理: 1.资源请求的判断: (1)fetch 事件会拦截页面上所有的网络资源请求,但我们通常只对部分资源请求进行处理, 其余的请求会继续走浏览器默认的资源请求流程 (2)f ...

  6. Linux_更改时区和利用Crontab同步时间

    一.更改时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 二.Crontab时间同步 crontab -e   #crontab编辑 */5 ...

  7. October 06th, 2019. Week 41st, Sunday

    Life is very capricious. 生命无常. Is life capricious? Maybe. But we can still make life a little more c ...

  8. Eureka工作原理及它和ZooKeeper的区别

    1.Eureka 简介: Eureka 是 Netflix 出品的用于实现服务注册和发现的工具. Spring Cloud 集成了 Eureka,并提供了开箱即用的支持.其中, Eureka 又可细分 ...

  9. BootStrap 关于input与btn的点击focus取消特效相关css

    取消btn按钮点击出现的外边框: .btn:focus, /*清除btn按钮点击出现的边框*/.btn:active:focus,.btn.active:focus,.btn.focus,.btn:a ...

  10. ETCD:基于角色的访问控制

    原文地址:Role-based access control 总览 身份验证已添加到etcd 2.1中. etcd v3 API略微修改了身份验证功能的API和用户界面,以更好地适应新的数据模型.本指 ...