A simple demo showing go routines and JS/Go integration.
$ npm install
$ npm run build
$ npm run serve
License Apache-2
| node_modules | |
| package-lock.json |
| <!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 <[email protected]>", | |
| "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) | |
| } | |
| } |
(Sorry about that, but we can’t show files that are this big right now.)