为了保护应用程序不崩溃,尝试使用异常处理的替代方案。

huangapple go评论113阅读模式
英文:

Try exception alternative for protect not crash application

问题

我有一个用于抓取URL的Go应用程序。问题是它有时会崩溃并返回以下错误:

  1. panic: runtime error: slice bounds out of range
  2. goroutine 1 [running]:
  3. main.dom6(0x187d4140, 0x8, 0x187d4179, 0x5, 0x187c0800, 0x6, 0x13, 0x83007cb)
  4. /root/sswork.go:326 +0x6b
  5. main.sub(0x187d4140, 0x8, 0x84464e0, 0x6, 0x6, 0x187d4140, 0x8, 0x187d4179, 0x5, 0x187c0800, ...)
  6. /root/sswork.go:298 +0xb3
  7. main.main()
  8. /root/sswork.go:615 +0xccb

在第298行是这个函数:

  1. 294: // try our list of substitutions, if one works return it
  2. 295: func sub(str string, xs []subs, u string, p string, h string) string {
  3. 296: for _, x := range xs {
  4. 297: if strings.Contains(str, x.pattern) {
  5. 298: return strings.Replace(str, x.pattern, x.fn(u, p, h), 1)
  6. 299: }
  7. 300: }
  8. 301: return str
  9. 302:}

如何解决这个问题,使应用程序不再崩溃?

  1. 324: // the first 6 characters of the above
  2. 325: func dom6(u string, p string, d string) string {
  3. 326: return domfull(u, p, d)[0:6]
  4. 327: }
英文:

I have a Go application for scrape URLs. The problem is it sometimes crashes and returns this error:

  1. panic: runtime error: slice bounds out of range
  2. goroutine 1 [running]:
  3. main.dom6(0x187d4140, 0x8, 0x187d4179, 0x5, 0x187c0800, 0x6, 0x13, 0x83007cb)
  4. /root/sswork.go:326 +0x6b
  5. main.sub(0x187d4140, 0x8, 0x84464e0, 0x6, 0x6, 0x187d4140, 0x8, 0x187d4179, 0x5, 0x187c0800, ...)
  6. /root/sswork.go:298 +0xb3
  7. main.main()
  8. /root/sswork.go:615 +0xccb

on line 298 is this function :

  1. 294: // try our list of substitutions, if one works return it
  2. 295: func sub(str string, xs []subs, u string, p string, h string) string {
  3. 296: for _, x := range xs {
  4. 297: if strings.Contains(str, x.pattern) {
  5. 298: return strings.Replace(str, x.pattern, x.fn(u, p, h), 1)
  6. 299: }
  7. 300: }
  8. 301: return str
  9. 302:}

How can I solve my problem so it doesn't crash the application any more?

  1. 324: // the first 6 characters of the above
  2. 325: func dom6(u string, p string, d string) string {
  3. 326: return domfull(u, p, d)[0:6]
  4. 327: }

答案1

得分: 2

错误出现在第326行,而不是第298行。为了避免这样的恐慌,应在尝试对切片、数组或字符串进行索引或切片之前进行手动索引检查。

您指出第298行的代码如下:

  1. // 上述代码的前6个字符
  2. func dom6(u string, p string, d string) string {
  3. return domfull(u, p, d)[0:6]
  4. }

在尝试对其进行切片之前,请检查domfull()返回的string的长度,例如:

  1. func dom6(u string, p string, d string) string {
  2. df := domfull(u, p, d)
  3. if len(df) < 6 {
  4. return df
  5. }
  6. return df[:6]
  7. }
英文:

The error is at line 326, not 298. And to avoid such panics, perform manual index check before attempting to index or slice a slice, array or string.

You indicated that the code at line 298 is:

  1. // the first 6 characters of the above
  2. func dom6(u string, p string, d string) string {
  3. return domfull(u, p, d)[0:6]
  4. }

Check the length of the string returned by domfull() before attempting to slice it, e.g.:

  1. func dom6(u string, p string, d string) string {
  2. df := domfull(u, p, d)
  3. if len(df) &lt; 6 {
  4. return df
  5. }
  6. return df[:6]
  7. }

huangapple
  • 本文由 发表于 2017年9月8日 18:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/46114325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定