Panic on customized Stringer of map
Author: wwwjfyCreated Sep 10, 2019Updated May 13, 2023
Hi, I've come across this issue, that with the pointer method feature, the address is updated to 0x1.
Demonstration:
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
)
type myMap map[int64]int64
func (c myMap) String() string {
fmt.Printf("pointer changed: %p\n", c)
for k := range c {
fmt.Println(k)
}
return "custom string"
}
func main() {
spew.Printf("%v\n", myMap{1: 1})
}It gives me
$ go run main.go
pointer changed: 0x1
(PANIC=runtime error: invalid memory address or nil pointer dereference)map[1:1]With spew.Config.DisablePointerMethods = true, the panic is gone.
Source: davecgh/go-spew