Skip to content

Struct field assigned nil != nil #639

Open
@theclapp

Description

@theclapp

Why doesn't t1.foo == nil, after I just assigned it nil? Why doesn't it even == itself?

type foo struct {
	*js.Object
	bar bool `js:"bar"`
}
type t struct {
	*js.Object
	*foo `js:"foo"`
}

t1 := t{Object: js.Global.Get("Object").New()}
t1.foo = nil

// => "t1.foo != nil"
if t1.foo == nil {
	fmt.Println("t1.foo == nil")
} else {
	fmt.Println("t1.foo != nil")
}
fmt.Printf("t1.foo is %v\n", t1.foo) // => "t1.foo is null"

// => "t1.foo != itself"
if t1.foo == t1.foo {
	fmt.Println("t1.foo == itself")
} else {
	fmt.Println("t1.foo != itself")
}

Playground link

Am I doing something wrong? Is there a better way to deal with this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixNeedsHelpCommunity contributions are welcome for this feature!bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions