gogolanddelve

A nil pointer panic occurs only in debug mode while calling fmt.Sprintf


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?


Solution

  • 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.