douhao2548 2019-08-27 14:19
浏览 48
已采纳

如何捕获“切片边界超出范围”错误并为其编写句柄

I read other questions about the "slice bounds of range" here in the stackoverflow, but none of them using the same context from this. Then, none of the answers helped me.

In my use case, the "golang's syntax" of substring using [] doesn't return an error variable. It launches a runtime error using the "panic" instruction. My goal is to avoid to reach on the "panic" instruction. I need to treat this error and provide messages that describe with more details the context around the moment where this error had occurred.

Obs: The content of the string variable which one I need to get the substring value is totally dynamic and the indexes that I have been using to get the substring value is equally calculated dynamically.

  • 写回答

1条回答 默认 最新

  • dsjlqkbpn029473708 2019-08-27 14:23
    关注

    You need to do bounds checking on your indexes:

    if j >= 0 && j <= len(str) {
       y = str[:j]
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程