切片边界超出范围错误,而且我没有任何切片变量。

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

slice bounds out of range error and i don't have any slice variable

问题

我正在处理一个项目,但是出现了一个错误,错误提示是“切片索引超出范围”,但是与该错误相关的代码行似乎并没有使用切片变量:

bt.blockDateTime = bt.getDate(bt.getDateBlockHeader(header[10:22]))
serviceType := hex2decimal(record[56:58])

以下是我创建并使用的函数:

func(bt *Hc34) getDateBlockHeader(input string) string{
        year := input[0:2]
        quant:= hex2decimal(input[3:6])
        hour := input[6:len(input)];
        /*if err!=nil{
            panic(err)
        }*/
        return "20" + year + bt.getQuantDate(quant) + "" + hour
    }
func(bt *Hc34) getDate(date string) string{
        year := date[0:4]
        month := date[4:6]
        day := date[6:8]
        hour := date[8:10]
        minute := date[10:12]
        second := date[12:14]
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }
func(bt *Hc34) getQuantDate(quant int) string{
        nanoDayOfYear:=strconv.FormatInt(int64(quant)*24*60*60*1000*1000*1000, 10) + "ns"
        durt,err:=time.ParseDuration(nanoDayOfYear)
        t := time.Date(2009, time.January, 01, 00, 0, 0, 0, time.UTC)
        t=t.Add(durt)
        if err!=nil{
            
            panic(err)
        }
        return string(t.Month())+""+string(t.Day())

    }
func hex2decimal(hexStr string) int{
    integer,_:=strconv.ParseInt(hexStr,0,64)
    return int(integer)

}

以下是出现错误的代码行:

panic: runtime error: slice bounds out of range

goroutine 1 [running]:
github.com/bob96/hc34/beater.(*Hc34).hc34decoderfunc(0xc420238380, 0xc4202cc000, 0x2d128)
	/home/hp/src/github.com/bob96/hc34/beater/hc34.go:190 +0xb60
github.com/bob96/hc34/beater.(*Hc34).hc34DataHolderfunc(0xc420238380, 0xc42016e3c0, 0x5e)
	/home/hp/src/github.com/bob96/hc34/beater/hc34.go:141 +0xd3
github.com/bob96/hc34/beater.(*Hc34).Run(0xc420238380, 0xc420176000, 0xc4201760a8, 0xb)
	/home/hp/src/github.com/bob96/hc34/beater/hc34.go:105 +0x109
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.(*Beat).launch(0xc420176000, 0x9a84e8, 0x0, 0x0)
	/home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:211 +0x706
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.Run(0x9855d5, 0x4, 0x0, 0x0, 0x9a84e8, 0xc4200001a0, 0xc4200001a0)
	/home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:136 +0x65
main.main()
	/home/hp/src/github.com/bob96/hc34/main.go:12 +0x54

请问有人可以帮助我吗?非常感谢!

英文:

i'm working on a project and i had an error taht say slice bounds out of range but the lines that are concerned with this error don't seem to be a slive variable:

bt.blockDateTime =bt.getDate(bt.getDateBlockHeader(header[10:22]))
serviceType := hex2decimal(record[56:58])

and here are the function that i have created that i have used :

func(bt *Hc34) getDateBlockHeader(input string) string{
        year := input[0:2]
        quant:= hex2decimal(input[3:6])
        hour := input[6:len(input)];
        /*if err!=nil{
            panic(err)
        }*/
        return "20" + year + bt.getQuantDate(quant) + "" + hour
    }
func(bt *Hc34) getDate(date string) string{
        year := date[0:4]
        month := date[4:6]
        day := date[6:8]
        hour := date[8:10]
        minute := date[10:12]
        second := date[12:14]
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }
func(bt *Hc34) getQuantDate(quant int) string{
        nanoDayOfYear:=strconv.FormatInt(int64(quant)*24*60*60*1000*1000*1000, 10) + "ns"
        durt,err:=time.ParseDuration(nanoDayOfYear)
        t := time.Date(2009, time.January, 01, 00, 0, 0, 0, time.UTC)
        t=t.Add(durt)
        if err!=nil{
            
            panic(err)
        }
        return string(t.Month())+""+string(t.Day())

    }
func hex2decimal(hexStr string) int{
    integer,_:=strconv.ParseInt(hexStr,0,64)
    return int(integer)

}

here is the error lines:
panic: runtime error: slice bounds out of range

goroutine 1 [running]:
github.com/bob96/hc34/beater.(*Hc34).hc34decoderfunc(0xc420238380, 0xc4202cc000, 0x2d128)
	/home/hp/src/github.com/bob96/hc34/beater/hc34.go:190 +0xb60
github.com/bob96/hc34/beater.(*Hc34).hc34DataHolderfunc(0xc420238380, 0xc42016e3c0, 0x5e)
	/home/hp/src/github.com/bob96/hc34/beater/hc34.go:141 +0xd3
github.com/bob96/hc34/beater.(*Hc34).Run(0xc420238380, 0xc420176000, 0xc4201760a8, 0xb)
	/home/hp/src/github.com/bob96/hc34/beater/hc34.go:105 +0x109
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.(*Beat).launch(0xc420176000, 0x9a84e8, 0x0, 0x0)
	/home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:211 +0x706
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.Run(0x9855d5, 0x4, 0x0, 0x0, 0x9a84e8, 0xc4200001a0, 0xc4200001a0)
	/home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:136 +0x65
main.main()
	/home/hp/src/github.com/bob96/hc34/main.go:12 +0x54

can some one help me please it will be a bless!

答案1

得分: 2

一个字符串在Go语言中是字符的切片。这就是为什么你可以像record[56:58]那样对字符串进行切片。

在切片之前,你需要检查字符串的长度,以避免出现错误。

可以像这样进行检查:

if len(string) < requiredLen {
  return
}
a := string[requiredLen-2:requiredLen]
英文:

A string is a slice of characters in Go. That is why you are able to slice the string like record[56:58].

You need to check the length of the string before slicing it to avoid panics.

Something like

if len(string)&lt;requiredLen{
  return
}
a := string[requiredLen-2:requiredLen]

答案2

得分: 2

好的,以下是翻译好的内容:

当你编写如下的代码时:

x := y[a:b]

实际上是在进行切片操作。我建议在对"header"和"record"进行任何处理之前,先检查它们的长度。

希望对你有帮助。

英文:

Well, whenever you write code like

x := y[a:b]

you are effectively slicing. I suggest you check the length of the "header" and "record" before you do any processing on it.

Hope it helps.

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

发表评论

匿名网友

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

确定