Skip to content

Instantly share code, notes, and snippets.

@kentquirk
Created June 28, 2018 03:38
Show Gist options
  • Save kentquirk/f6ec7d48df1220ade326d3641fc9bd7a to your computer and use it in GitHub Desktop.
Save kentquirk/f6ec7d48df1220ade326d3641fc9bd7a to your computer and use it in GitHub Desktop.
Using a Go struct to shadow a JS Object
package main
//go:generate gopherjs build --minify
import (
"crypto/sha256"
"github.com/gopherjs/gopherjs/js"
)
func main() {
js.Module.Get("exports").Set("hashobj", hashobj)
}
type UserInfo struct {
*js.Object
Name string `js:"name"`
Address string `js:"address"`
City string `js:"city"`
State string `js:"state"`
Hash []byte `js:"hash"`
}
func hashobj(obj *js.Object) *js.Object {
// Get a UserInfo from the passed-in object
ui := &UserInfo{Object: obj}
h := sha256.New()
h.Write([]byte(ui.Name))
h.Write([]byte(ui.Address))
h.Write([]byte(ui.City))
ui.Hash = h.Sum(nil)
return ui.Object
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment