gopherjs / gopherjs/gopherjs

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

Open
#625 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

NeedsDecision
Dominant language
Go
Stars
13.2k
Forks
573
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the example program from the issue in the linked GopherJS playground and compare its output for nil *js.Object values with a nil user-defined struct pointer. Trace the formatting behavior for direct and struct-contained values, then verify that both cases produce consistent nil output without the reported null or 0x1 results.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.