WebAssembly is one of the modern technologies designed for running more languages on the web client with Javascript interoperability.
Also, interoperate with Javascript on the web.
Youll need to make a few configurations to transpile your Go code into WebAssembly.
Youll have to change your Go architectureGOARCHenvironment variable towasmand Go operating systemGOOSvariable tojs.
Run this command in the terminal of your working directory to make these configurations.
The next step is to transpile your Go code into a WebAssembly.wasmfile.
The command copies thewasm_exec.jsfile to your working directory and serves as the entry point to your tool.
you could now use thewasm_exec.jsscript to execute your WASM files with Go and makeDOM APIcalls.
Starting a Web Server to execute the Page
Youllset up the server with thehttppackage.
Import thehttppackage and thelogpackage for logging possible errors to the console.
you’re free to use theFileServermethod of thehttppackage to serve files in a specified directory.
TheFileServermethod takes in the directory and returns a file server instance.
In themainfunction, you declared a file server instance variable to serve the files in the root directory.
TheListenAndServemethod serves the files in the specified directory on the specified port.
Thejspackage provides access to WebAssembly host environments on thejs/wasmarchitecture.
Youll need to have your development environment set up to theGOARCH=wasm GOOS=jsto access and use the package.
you’ve got the option to use the various methods in the package to interact with your webpage.
Heres how it’s possible for you to register functions with thejspackage.
you might register call-back functions with theSetmethod of theGlobalmethod of thejspackage.
TheSetmethod takes in the function identifier and a call-back function instance.
TheRegisterCallbackFunctionsmethod registers theprintfunction as a call-back function that you’re free to use in the web app console.
Thejspackage is experimental, and the package is exempt from the Go compatibility promise.