Quick story:
I’ve an html file that’s utilizing WebAssembly file, every little thing is working easily if the html is loaded as a static file utilizing Golang server, but when the html is loaded as a template then the WebAssembly file shouldn’t be working, and I get the error:
localhost/:1 Uncaught (in promise) CompileError: WebAssembly.compile(): anticipated magic phrase 00 61 73 6d, discovered 3c 21 44 4f @+0
Lengthy story:
I’ve the under html file:
<!DOCTYPE html>
<html>
<head>
<title>Golang WebAssembly Instance</title>
</head>
<physique>
<button onclick="handleClick()">Click on me</button>
<button onclick="callHandleData()">handleData</button>
<button onclick="notifyme()">notifyme</button>
<div id="outcome"></div>
<script src="https://discussion board.golangbridge.org/script/wasm_exec.js"></script>
<script>
const go = new Go();
let wasm;
fetch('predominant.wasm').then(response =>
response.arrayBuffer()
).then(bytes => WebAssembly.instantiate(bytes, go.importObject)).then(outcome => {
wasm = outcome.occasion;
go.run(wasm); // This can begin the Go runtime
})
operate callHandleData() {
let information = handleData(46, "Hiya, World!");
// Parse the info utilizing JSON.parse // Parse the info utilizing JSON.parse
let parsedData = JSON.parse(information); // Double stringify for security
// Entry the textual content property from the parsed object
let textual content = parsedData.textual content;
let num = parsedData.quantity;
alert(information)
alert(textual content + " What's the "+ num); // Or use textual content for additional manipulation
}
</script>
</physique>
</html>
I’m attempting to load it from a template in a Golang as under, however I’m getting the talked about error:
package deal predominant
import (
"html/template"
"internet/http"
"path/filepath"
)
func handler(w http.ResponseWriter, r *http.Request) {
baseTmpl, err := template.ParseFiles("wasm.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = baseTmpl.ExecuteTemplate(w, "wasm.html", nil) // Move nil as information
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func predominant() {
http.HandleFunc("https://discussion board.golangbridge.org/", handler)
// Serve static information
http.HandleFunc("/script/", func(w http.ResponseWriter, r *http.Request) {
filePath := r.URL.Path[len("/script/"):]
if filepath.Ext(filePath) == ".wasm" {
w.Header().Set("Content material-Kind", "utility/wasm")
}
http.ServeFile(w, r, "script/"+filePath)
})
http.ListenAndServe(":8080", nil)
}