英文:
A nil pointer panic occurs only in debug mode while calling fmt.Sprintf
问题
你使用的Go版本是什么(go version)?
$ go version
go version go1.18 darwin/amd64
你做了什么?
我写了一些简单的代码,尝试打印一个结构体。
import v1 "k8s.io/api/core/v1"
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
func main() {
data := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
DeletionTimestamp: nil,
},
}
str := fmt.Sprintf("%#v", data)
fmt.Println(str)
}
你期望看到什么?
与或没有调试器相同的行为。
你看到了什么?
我打算打印一个名为Namespace的结构体,该结构体在k8s.io/api/core/v1/types.go中定义。当我在GoLand(GoLand 2021.3.4)中简单运行它而不使用调试时,它按预期运行,并且对象的结构已在控制台中打印出来。但是,当我在带有调试器的GoLand中运行它时,程序被错误中断,错误消息为“bad access: nil dereference”。调用堆栈如下:
<autogenerated>:2
fmt.(*pp).handleMethods (print.go:603) fmt
fmt.(*pp).printValue (print.go:723) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printArg (print.go:712) fmt
fmt.(*pp).doPrintf (print.go:1026) fmt
fmt.Sprintf (print.go:219) fmt
main.main (main.go:97) main
runtime.main (proc.go:250) runtime
runtime.goexit (asm_amd64.s:1571) runtime
- Async Stack Trace
<autogenerated>:2
这有点奇怪:如果这个(panic)是预期的行为,那么为什么没有调试器时它可以正确运行?
英文:
What version of Go are you using (go version)?
$ go version
go version go1.18 darwin/amd64
What did you do?
I wrote some simple code below that it try to print a struct
import v1 "k8s.io/api/core/v1"
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
func main() {
data := v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
DeletionTimestamp: nil,
},
}
str := fmt.Sprintf("%#v", data)
fmt.Println(str)
}
What did you expect to see?
same behavior with or without debugger
What did you see instead?
I was intended to print a struct called Namespace which is defined in k8s.io/api/core/v1/types.go, when I run it simply in GoLand(GoLand 2021.3.4) without debug, it runs as expected and the structure of the object had been printed in the console, but when i run it also in Goland but with debug, the program had been interrupted by an error: "bad access: nil dereference". the call stack is:
<autogenerated>:2
fmt.(*pp).handleMethods (print.go:603) fmt
fmt.(*pp).printValue (print.go:723) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printValue (print.go:806) fmt
fmt.(*pp).printArg (print.go:712) fmt
fmt.(*pp).doPrintf (print.go:1026) fmt
fmt.Sprintf (print.go:219) fmt
main.main (main.go:97) main
runtime.main (proc.go:250) runtime
runtime.goexit (asm_amd64.s:1571) runtime
- Async Stack Trace
<autogenerated>:2
this is a little bit weird: If this(panic) is a intented behavior, then how can it runs correctly without debugger?
答案1
得分: 0
我遇到了同样的问题。我尝试升级Mac OS版本,但没有成功。
主要原因是debugserver版本,我之前的版本是:
/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/debugserver --version
debugserver-@(#)PROGRAM:LLDB PROJECT:lldb-1205.0.27
然后,我通过以下方式升级debugserver:
sudo rm -rf /Library/Developer/CommandLineTools
然后它会重新安装,最新版本是:
debugserver-@(#)PROGRAM:LLDB PROJECT:lldb-1316.0.9.46
之后,一切正常工作了。
英文:
I had the same problem. I tried to upgrade the mac os version, but it did not work.
The main reason is debugserver version,mine before version is:
/Library/Developer/CommandLineTools/Library/PrivateFrameworks/LLDB.framework/Versions/A/Resources/debugserver --version
debugserver-@(#)PROGRAM:LLDB PROJECT:lldb-1205.0.27
and, later upgrade debugserver by:
sudo rm -rf /Library/Developer/CommandLineTools
and it will remain to intall again, and the latest version is:
debugserver-@(#)PROGRAM:LLDB PROJECT:lldb-1316.0.9.46
then, everything works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论