Skip to content

Instantly share code, notes, and snippets.

@kentquirk
Created June 28, 2018 03:16
Show Gist options
  • Save kentquirk/e141acb51214e536f242e6707d32e85f to your computer and use it in GitHub Desktop.
Save kentquirk/e141acb51214e536f242e6707d32e85f to your computer and use it in GitHub Desktop.
How to use an object sent by JS in gopherjs
package main
//go:generate gopherjs build --minify
import (
"crypto/sha256"
"github.com/gopherjs/gopherjs/js"
)
func main() {
js.Module.Get("exports").Set("hashobj", hashobj)
}
func hashobj(obj map[string]interface{}) []byte {
h := sha256.New()
for _, k := range []string{"name", "address", "city"} {
if s, ok := obj[k]; ok {
switch st := s.(type) {
case string:
h.Write([]byte(st))
case []byte:
h.Write(st)
default:
// ignore types we don't expect
}
}
}
return h.Sum(nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment