you need to have golang installed on your machine
below steps will create a simple webassembly program in golang which is called from javascript in browser
after running below steps you will see 5 + 3 = 8 in browser console to confirm that golang code is being called from javascript in browser
cat main.go
// main.go
package main
import (
"syscall/js"
)
func add(this js.Value, args []js.Value) interface{} {
a, b := args[0].Int(), args[1].Int()
return a + b
}
func main() {
js.Global().Set("add", js.FuncOf(add))
select {} // Keep the program running
}
GOOS=js GOARCH=wasm go build -o main.wasm main.go
which can be shown by running command
echo $(dirname $(dirname $(readlink -f $(which go))))/misc/wasm/wasm_exec.js
so lets copy this file into our local dir to make visable to our code
cp $(dirname $(dirname $(readlink -f $(which go))))/misc/wasm/wasm_exec.js .
NOTICE in above the command ends with a period to copy file into local dir
cat index.html
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go WebAssembly Example</title>
<script src="wasm_exec.js"></script>
</head>
<body>
<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
go.run(result.instance);
// Example usage of the Go function in JavaScript
let sum = add(5, 3);
console.log("5 + 3 =", sum);
});
</script>
</body>
</html>
cat local_server.go
package main
import (
"net/http"
)
func main() {
http.ListenAndServe(":8080", http.FileServer(http.Dir(".")))
}
go run local_server.go
5 + 3 = 8