Skip to content

Instantly share code, notes, and snippets.

@surma
Last active April 5, 2019 09:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save surma/fb1785431c26589bc7c6594d3dd69744 to your computer and use it in GitHub Desktop.
Save surma/fb1785431c26589bc7c6594d3dd69744 to your computer and use it in GitHub Desktop.
GoWasm
node_modules
package-lock.json

A simple demo showing go routines and JS/Go integration.

Build

$ npm install
$ npm run build
$ npm run serve

License Apache-2

<!doctype html>
<!--
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<pre></pre>
<script src="wasm_exec.js"></script>
<script>
const pre = document.querySelector('pre');
function goCallback(v) {
pre.textContent += `${v}\n`;
}
async function init() {
const go = new Go();
const {instance} = await WebAssembly.instantiateStreaming(fetch("/sieve.wasm"), go.importObject);
await go.run(instance);
}
init();
</script>
{
"name": "wasmgo",
"version": "0.0.1",
"description": "Simple Go wasm demo",
"scripts": {
"build": "GOOS=js GOARCH=wasm go build -o sieve.wasm sieve.go",
"serve": "http-server -c0"
},
"author": "Surma <surma@google.com>",
"license": "Apache-2.0",
"devDependencies": {
"http-server": "^0.11.1"
}
}
/**
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"log"
"syscall/js"
)
func filter(i int64, cIn <-chan int64) <-chan int64 {
cOut := make(chan int64)
go func() {
for v := range cIn {
if v%i != 0 {
cOut <- v
}
}
close(cOut)
}()
return cOut
}
func generator(start int64, end int64) <-chan int64 {
cOut := make(chan int64)
go func() {
for i := start; i < end; i++ {
cOut <- i
}
close(cOut)
}()
return cOut
}
func main() {
callback := js.Global().Get("goCallback")
if callback == js.Undefined() {
log.Fatalf("No `goCallback` found on global")
}
out := generator(2, 1000)
var v int64
var ok bool
for {
v, ok = <-out
if !ok {
return
}
callback.Invoke(v)
out = filter(v, out)
}
}
This file has been truncated, but you can view the full file.
View raw

(Sorry about that, but we can’t show files that are this big right now.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment