Skip to content

Instantly share code, notes, and snippets.

@andrewmkano
Last active January 26, 2023 14:47
Show Gist options
  • Save andrewmkano/2cdc0e2855b2a617d64eab72429d57ad to your computer and use it in GitHub Desktop.
Save andrewmkano/2cdc0e2855b2a617d64eab72429d57ad to your computer and use it in GitHub Desktop.
WebAssembly example with Go
package callbacks
import (
"fmt"
"syscall/js"
)
func HasRepeatedChars(this js.Value, args []js.Value) any {
if len(args) > 1 {
fmt.Println("This callback only needs a single argument")
return nil
}
val := args[0].String()
r := hasRepeatedChars(val)
if r {
fmt.Println("Yes, it does")
return true
}
fmt.Println("No, it does not")
return false
}
package callbacks
import (
"fmt"
"syscall/js"
)
func HasRepeatedChars(this js.Value, args []js.Value) any {
if len(args) > 2 {
fmt.Println("This callback only requires two arguments. One for the id of the input and another one for its result")
return nil
}
resId := args[1].String()
valId := args[0].String()
inp := js.Global().Get("document").Call("getElementById", valId).Get("value").String()
r := hasRepeatedChars(inp)
if r {
fmt.Println("Yes, there is at least character repeating once")
updateResultBoxByID(resId, "Yes, there is at least character repeating once")
return true
}
fmt.Println("No, none of the characters is repeating")
updateResultBoxByID(resId, "No, none of the characters is repeating")
return false
}
func Clear(this js.Value, args []js.Value) any {
for _, arg := range args {
if arg.IsUndefined() || arg.IsNull() {
continue
}
updateResultBoxByID(arg.String(), "")
}
return nil
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Go WebAssembly</title>
</head>
<body>
<script src="wasm_exec.js"></script>
<script src="go.js"></script>
<h2>Repeated Characters Check</h2>
<input type="text" id="val1"/>
<button onClick="hasRepeatedChars('val1','result');" id="execButton">Check</button>
<button onClick="reset('val1','result');" id="resetButton">Clear</button>
<input type="text" id="result" disabled style="min-width: 300px;"/>
</body>
</html>
const go = new Go();
let mod, wasm;
WebAssembly.instantiateStreaming(fetch("wasm.wasm"),go.importObject).then(
result => {
mod = result.module;
wasm = result.instance;
});
async function run() {
console.clear();
await go.run(wasm);
wasm = await WebAssembly.instantiate(mod,go.importObject);
}
const go = new Go();
let mod, wasm;
WebAssembly.instantiateStreaming(fetch("wasm.wasm"),go.importObject).then(
async result => {
mod = result.module;
wasm = result.instance;
await go.run(wasm);
});
package callbacks
import "fmt"
func hasRepeatedChars(val string) bool {
m := make(map[string]bool)
for _, character := range val {
char := fmt.Sprintf("%c", character)
e := m[char]
if e {
return true
}
m[char] = true
}
return false
}
package callbacks
import (
"fmt"
"syscall/js"
)
func hasRepeatedChars(val string) bool {
m := make(map[string]bool)
for _, character := range val {
char := fmt.Sprintf("%c", character)
e := m[char]
if e {
return true
}
m[char] = true
}
return false
}
func updateResultBoxByID(boxID, v string) {
js.Global().Get("document").Call("getElementById", boxID).Set("value", v)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Go WebAssembly</title>
</head>
<body>
<button onClick="run();" id="runButton">Run</button>
<script src="wasm_exec.js"></script>
<script src="go.js"></script>
</body>
</html>
package main
import (
"fmt"
)
func main() {
fmt.Println("WASM Program Online!")
}
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
fmt.Println("Starting server on 8080...")
log.Fatal(
http.ListenAndServe(":8080",
http.FileServer(
http.Dir("./"),
),
),
)
}
package main
import (
"fmt"
"github/amartine59/wasm-example/prog/callbacks"
"syscall/js"
)
func main() {
ch := make(chan struct{})
fmt.Println("WASM Program Online!")
js.Global().Set("hasRepeatedChars", js.FuncOf(callbacks.HasRepeatedChars))
<-ch
}
package main
import (
"fmt"
"github/amartine59/wasm-example/prog/callbacks"
"syscall/js"
)
func main() {
ch := make(chan struct{})
fmt.Println("WASM Program Online!")
js.Global().Set("hasRepeatedChars", js.FuncOf(callbacks.HasRepeatedChars))
js.Global().Set("reset", js.FuncOf(callbacks.Clear))
<-ch
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment