普通网友 2016-02-28 06:46
浏览 101
已采纳

欺骗以快速查找在Go中引发错误的文件和行号?

In my journey with go discovered that there are no stacktraces. so whenever something breaks, all we get an simple string error message without any information where is this is coming from. This is in stark contrast with other languages where I am used to seing detailed stacktraces

For example, below is the error message from apex

$ cat event.json | apex invoke --logs webhook                                  
   ⨯ error parsing response: json: cannot unmarshal array into Go value of type map[string]interface {}

here its telling me that unmarshal to a map ins't working because event.json is an array. We have unmarshal to interface{} to support both arrays & maps.However, it doesn't tell me which file/line is causing this error.

Questions:

  1. What is way to quickly find which file/line this error coming from?
  2. In General, Are there tips/tricks which gophers use to get to the source of problem quickly from this string error message?
  3. is this how stack traces are for most go projects or there are any best practices that should be followed?
  • 写回答

3条回答 默认 最新

  • dousuie2222 2016-03-04 10:27
    关注

    What is way to quickly find which file/line this error coming from?

    There are no default stacks printed unless it's an unrecovered panic.

    In General, Are there tips/tricks which gophers use to get to the source of problem quickly from this string error message? is this how stack traces are for most go projects or there are any best practices that should be followed?

    In General, you need to check error returns from most of the function calls. There are more than one way to do that. I usually use standard library package log to print out error logs with file and line numbers for easy debugging in simple programs. For example:

    package main
    
    import "log"
    import "errors"
    
    func init() { log.SetFlags(log.Lshortfile | log.LstdFlags) }
    
    func callFunc() error {
        return errors.New("error")
    }
    
    func main() {
        if err := callFunc(); err != nil {
            log.Println(err)
        }
    }
    

    http://play.golang.org/p/0iytNw7eZ7

    output:

    2009/11/10 23:00:00 main.go:14: error
    

    Also, there are functions for you to print or retrieve current stacks in standard library runtime/debug, e.g. https://golang.org/pkg/runtime/debug/#PrintStack

    There are many community efforts on bringing error handling easier, you can search error in GoDoc: https://godoc.org/?q=error

    Hope these helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥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,如何解決?