*js.Error and *js.Object treating null objects differently

46 views
Skip to first unread message

Jonathan Hall

unread,
Aug 21, 2015, 12:13:42 PM8/21/15
to GopherJS
I'm not sure if I've run into a bug, or if I'm misusing GopherJS, but I've run into a problem with the way my Go code receives errors passed by Javascript Callbacks.  To demonstrate the problem, I've built this code:

My main.js:

window.foo = {
    nothing: function(cb) {
        return cb(null);
    }
}
require('main');

And main.go:

package main

import (
)

func main() {
    result := nothing()
    console.Log(result)
    if result == nil {
        console.Log("result is nil")
    }
    if result != nil {
        console.Log("result is not nil")
    }
}

func nothing() *js.Error {
    c := make(chan *js.Error)
    go func() {
        js.Global.Get("foo").Call("nothing",func(err *js.Error) {
            c <- err
        })
    }()
    return <-c
}

The output on my javascript console (in Chrome 43, FWIW) is:

null
main.js:2622 result is not nil

If I do a s/*js.Error/*js.Object/ in main.go, I get the expected result:

null
main.js:2622result is nil


So it seems that somehow the Error struct around *js.Object is obfuscating the nullness of the javascript object?  Am I doing this wrong?

Joop Kiefte

unread,
Jan 15, 2016, 4:50:46 AM1/15/16
to GopherJS
the value of the *js.Error struct is actually { Object: nil }

Op vrijdag 21 augustus 2015 18:13:42 UTC+2 schreef Jonathan Hall:
Reply all
Reply to author
Forward
0 new messages