在调用fmt.Sprintf时,只有在调试模式下才会发生空指针恐慌。

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

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 &quot;k8s.io/api/core/v1&quot;
import metav1 &quot;k8s.io/apimachinery/pkg/apis/meta/v1&quot;

func main() {
	data := v1.Namespace{
		ObjectMeta: metav1.ObjectMeta{
			DeletionTimestamp: nil,
		},
	}
	str := fmt.Sprintf(&quot;%#v&quot;, 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:

&lt;autogenerated&gt;: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
&lt;autogenerated&gt;: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.

huangapple
  • 本文由 发表于 2022年4月30日 14:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/72066098.html
匿名

发表评论

匿名网友

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

确定