在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天编程网就整理分享《什么会导致 fmt.Sprintf 中的引用指针偶尔出现恐慌》,聊聊,希望可以帮助到正在努力赚钱的你。
问题内容当我在生产环境中使用fmt.sprintf格式化字符串对象时,偶尔会出现这样的panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal sigsegv: segmentation violation code=0x1 addr=0x0 pc=0x469772]
goroutine 225594643 [running]:
fmt.(*buffer).writestring(...)
/home/compile/makepkg_go/go/src/fmt/print.go:82
fmt.(*fmt).padstring(0xc000112c70, 0x0, 0x7)
/home/compile/makepkg_go/go/src/fmt/format.go:110 +0x8c
fmt.(*fmt).fmts(0xc000112c70, 0x0, 0x7)
/home/compile/makepkg_go/go/src/fmt/format.go:359 +0x61
fmt.(*pp).fmtstring(0xc000112c30, 0x0, 0x7, 0x73)
/home/compile/makepkg_go/go/src/fmt/print.go:450 +0x1ba
fmt.(*pp).printarg(0xc000112c30, 0x9b4360, 0xc00193a930, 0x73)
/home/compile/makepkg_go/go/src/fmt/print.go:698 +0x843
fmt.(*pp).doprintf(0xc000112c30, 0xaa4b6c, 0x1a, 0xc00169deb8, 0x9, 0x9)
/home/compile/makepkg_go/go/src/fmt/print.go:1030 +0x15a
fmt.sprintf(0xaa4b6c, 0x1a, 0xc00169deb8, 0x9, 0x9, 0xc001edced0, 0xa)
/home/compile/makepkg_go/go/src/fmt/print.go:219 +0x66
code.aliyun.com/re-audio/engines/simplestatisticengine.audiocountrun(0xc001ca7a00, 0x14, 0x0, 0x7, 0xa9
8fec, 0xa, 0xa98dd0, 0xa, 0xc0018e329b, 0x5, ...)
/home/compile/makepkg/re-audio/engines/simplestatisticengine/sse.go:253 +0x384
created by code.aliyun.com/re-audio/audioprocessor.(*audioclips).clipsstatistic
/home/compile/makepkg/re-audio/audioprocessor/audio-clips.go:549 +0x3a0
相关代码是:
func AudioCountRun(
organization, appId, serviceId,
pattern, channel, soundproperty string, riskType int,
riskLevel, reqId string) {
if len(organization) == 0 || len(appId) == 0 || len(serviceId) == 0 || len(pattern) == 0 || len(channel) == 0 || len(soundproperty) == 0 || riskType < 0 || len(riskLevel) == 0 {
return
}
if serviceId != "POST_AUDIO" {
return
}
curTime := time.Now().Format(layout)
key := fmt.Sprintf("%s@%s@%s@%s@%s@%s@%s@%d@%s",
curTime, organization, appId,
serviceId, pattern, channel,
soundproperty, riskType, riskLevel)
batchstatistic.Add(audioMapName, key, 1)
ilog.LogReq(ilog.LL_INFO, "SimpleStatisticEngine.AudioCountRun.Add", reqId, fmt.Sprintf("(audioMapName=%s,key=%s)", audioMapName, key))
}
这个问题很难重复。我猜想 audiocountrun 中的“appid”参数有问题。不确定appid的类型是对的,但是它的数据是错误的。 我应该从什么方向寻找这个问题的原因?
解决方案
应该看到这个问题 https://github.com/golang/go/issues/39587
您的程序中的 audioMapName
或 key
上存在数据争用
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注编程网公众号,一起学习编程~