dongyan7950 2019-09-15 15:50
浏览 162

堆栈跟踪和golang错误。Unwrap()

I would like to build a stack trace that includes a low level db error with a second error which is human readable.

Is the new errors.Unwrap() function in golang 1.13 built for this purpose? Not sure I understand how to use it. Looking for an example on how to do this.

// model/book.go
package model

type Book struct {
    Id     uint32  `json:"id"     db:"id"`
    Title  string  `json:"title"  db:"title"`
    Author string  `json:"author" db:"author"`
    Price  float32 `json:"price"  db:"price"`
}

func (b *Book) Tablename() string {
    return "books"
}


// main.go
package main

func main() {
    bk := model.Book{
        Title:  "oliver twist",
        Author: "charles dickens",
        Price:  10.99,
    }

    err:= Create(&bk)
    if err !=nil {
        // how to use Unwrap?
    }

}
func Create(book *model.Book) error {
    insertSQL := "INSERT INTO ...."
    // code to insert
    if err != nil {
        return err
    }
    book.Id = uint32(lastID)
    return nil
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog