Closed phantoo0809 closed 6 years ago
echo: http: panic serving 127.0.0.1:36538: runtime error: index out of range
/home/work/open-falcon/chat/main.go:101 +0xdec
看错误提示 大概是 数组超界了 但是101行代码并没有数组取值啊 你是修改源码了吗
是的,我就是在main.go中把告警内容重新组合了,添加了如下部分: //告警内容格式修整
level:="告警级别: "+strings.FieldsFunc(text.Content, split)[0]
status:="采集项状态: "+strings.FieldsFunc(text.Content, split)[1]
macinfo:="服务器信息: "+strings.FieldsFunc(text.Content, split)[2]
warn:=strings.FieldsFunc(text.Content, split)[3]
warn=strings.Replace(warn, " ", "\n", -1)
caijixiang:="采集项: "+strings.FieldsFunc(warn, split)[2]+" "+strings.FieldsFunc(warn, split)[3]
value:="采集值: "+strings.FieldsFunc(warn, split)[4]+" "+strings.FieldsFunc(warn, split)[1]
warninfo:="采集项问题信息: "+strings.FieldsFunc(warn, split)[0]
time:="时间:"+strings.FieldsFunc(text.Content, split)[4]
info:=status+"\n"+macinfo+"\n"+caijixiang+"\n"+value+"\n"+warninfo+"\n"+level+"\n"+time
var co Content
co.Content=info
msg := MsgPost{
ToUser: toUser,
MsgType: "text",
AgentID: StringToInt(agentId),
Text: co,
}
数组取值之前 先判断数组元素数量 比如你要取warn[3]的值 首先就要判断 warn是否大于或者等于4 这样才能避免 数组超界导致的panic
是需要先判断warn的长度么?
这就是golang基础语法的问题了 我们就不继续沟通了 可以再多看下基础
好的,谢谢
echo: http: panic serving 127.0.0.1:36538: runtime error: index out of range goroutine 157 [running]: net/http.(conn).serve.func1(0xc420180640) /usr/lib/golang/src/net/http/server.go:1721 +0xd0 panic(0x7023e0, 0x8da480) /usr/lib/golang/src/runtime/panic.go:489 +0x2cf main.SendMsg(0x8be260, 0xc420176150, 0xc420034b58, 0x4c5212) /home/work/open-falcon/chat/main.go:101 +0xdec github.com/labstack/echo.(Echo).Add.func1(0x8be260, 0xc420176150, 0x8e4c20, 0x7fec42475000) /usr/lib/golang/src/github.com/labstack/echo/echo.go:479 +0x90 github.com/labstack/echo/middleware.LoggerWithConfig.func2.1(0x8be260, 0xc420176150, 0x0, 0x0) /usr/lib/golang/src/github.com/labstack/echo/middleware/logger.go:116 +0x12b github.com/labstack/echo.(Echo).ServeHTTP.func1(0x8be260, 0xc420176150, 0xc42007b238, 0x75cea0) /usr/lib/golang/src/github.com/labstack/echo/echo.go:576 +0x10e github.com/labstack/echo.(Echo).ServeHTTP(0xc42007b1e0, 0x8b7960, 0xc4201aa540, 0xc42015e100) /usr/lib/golang/src/github.com/labstack/echo/echo.go:585 +0x2a9 net/http.serverHandler.ServeHTTP(0xc4200a4420, 0x8b7960, 0xc4201aa540, 0xc42015e100) /usr/lib/golang/src/net/http/server.go:2568 +0x92 net/http.(conn).serve(0xc420180640, 0x8b7fa0, 0xc420326240) /usr/lib/golang/src/net/http/server.go:1825 +0x612 created by net/http.(Server).Serve /usr/lib/golang/src/net/http/server.go:2668 +0x2ce
上面是报错,请问这是什么原因呢?