Created
June 28, 2018 03:16
-
-
Save kentquirk/e141acb51214e536f242e6707d32e85f to your computer and use it in GitHub Desktop.
How to use an object sent by JS in gopherjs
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) | |
} | |
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