dty98339 2019-04-30 14:36
浏览 28

功能崩溃前如何写到控制台?

In C you can use the function write() from the unistd.h library. write() is faster than printf(), and allows you to write to the standard output (or a file) before a Segfault breaks your code.

When debugging, I wish to write to the standard output before my Go code panics. In general, how do I do that?

I have the following code (to find the shortest word in a string of words) which is panicking and I want to isolate where, by inserting write methods.

func FindShort(s string) int {
  i := 0
  j := 0
  min := math.MaxInt32
  for true {
    for s[i] == ' ' {
      i++
      j++
    }
    for s[j] != ' ' && j < len(s) {
      j++
    }
    if j > i && (j - i) < min {
      min = j - i
    }
    i = j
    if j == len(s) {
      break
    }
  }
  return min
}
  • 写回答

2条回答 默认 最新

  • drmqzb5063 2019-04-30 14:59
    关注

    you could use a defered function that calls the recover function, the function below will result in "Recovered panic here"

    defer func() {
        r := recover()
        if r != nil {
            fmt.Println("Recovered", r)
        }
    }()
    
    panic("panic here")
    
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂