Skip to content

fmt: Different behavior in printing of nil value *js.Object vs. regular struct #625

Open
@emidoots

Description

@emidoots

The way that a nil *js.Object is printed is different than the way that a nil user-defined struct is printed. I ran into this when attempting to use fmt.Sprintf("%+v", someJSObject) for test comparison purposes.

For example, this program on the playground (https://gopherjs.github.io/playground/#/MP9gVpB6AP):

package main

import (
	"fmt"

	"github.com/gopherjs/gopherjs/js"
)

type Object struct{}

func main() {
	fmt.Println("Printing a nil *Object (regular struct):")
	a := struct {
		o *Object
	}{}
	fmt.Printf("a.o: %+v\n", a.o)
	fmt.Printf("a: %+v\n", a)

	fmt.Println("\nPrinting a nil *js.Object:")
	b := struct {
		o *js.Object
	}{}
	fmt.Printf("b.o: %+v\n", b.o)
	fmt.Printf("b: %+v\n", b)
}

Produces:

Printing a nil *Object (regular struct): 
a.o: <nil> 
a: {o:<nil>} 
 
Printing a nil *js.Object: 
b.o: null 
b: {o:0x1} 

i.e. a nil *js.Object is printed as null instead of nil on its own, and when inside a struct it is printed as 0x1 despite being nil.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions