Created
June 28, 2018 03:38
-
-
Save kentquirk/f6ec7d48df1220ade326d3641fc9bd7a to your computer and use it in GitHub Desktop.
Using a Go struct to shadow a JS Object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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