2

[Golang] undefined Test in GopherJS

 2 years ago
source link: http://siongui.github.io/2016/02/06/go-undefined-test-in-gopherjs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[Golang] undefined Test in GopherJS

February 06, 2016

Check if something (object, variable, API, ...) is undefined or not in GopherJS. Use js.Undefined in your if test.

For example, you can check if your browser supports localStorage API or not by:

package main

import (
        "fmt"

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

func main() {
        if js.Global.Get("localStorage") == js.Undefined {
                fmt.Println("Your browser does not support localStorage API")
        } else {
                fmt.Println("Your browser supports localStorage API")
        }
}

Run Code on GopherJS Playground

For null test (if a variable is null), see [3].


References:

[2]Search undefined Results in GopherJS repo · GitHub


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK