In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to build the wasm environment in the golang language. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Golang installation
Download from the official address. MacOS can also be quickly installed through brew:
$brew install golang$ go versiongo version go1.17.2 darwin/arm64golang environment test
Create a new file main.go and write:
Package mainimport "fmt" func main () {fmt.Println ("Hello World!")}
Execute go run main.go, which outputs:
$go run main.go
Hello World!
If GO MODULES is enabled, you need to use the go mode init initialization module, or set up GO111MODULE=auto.
Package golang as WASM
There are usually two packaging methods, one that comes with golang and the other using tinygo. Tinygo is recommended because the compiled wasm module is smaller.
Using golang native compilation
Before compiling the wasm module, you need to set the golang cross-compilation parameters, the target system GOOS=js and target architecture GOARCH=wasm, and compile the wasm module:
/ / macos$ GOOS=js GOARCH=wasm go build-o main.wasm// windows temporarily sets golang environment parameters (only for the current CMD) $set GOOS=js $set GOARCH=wasm$ go build-o main.wasm
Compile with tinygo
You can install it directly according to the official documentation. MacOS is as follows:
$brew tap tinygo-org/tools$ brew install tinygo$ tinygo versiontinygo version 0.20.0 darwin/amd64 (using go version go1.17.2 and LLVM version 11.0.0)
Package main.go again using the following command:
$tinygo build-o main-tiny.wasm
Comparison of packaged file size
$du-sh. / * .wasm228K. / main-tiny.wasm1.9M. / main.wasm runs in the browser
In order to run main.wasm in the browser, we first need the JS glue code, which has been provided for us by golang and copied directly. It should be noted that the glue code compiled using tinygo is different from that compiled natively. Copy the corresponding code according to the specific situation:
/ / Native compilation $cp "$(go env GOROOT) / misc/wasm/wasm_exec.js". / / tinygo compilation $cp "$(tinygo env TINYGOROOT) / targets/wasm_exec.js". / wasm_exec_tiny.js
Second, you need a HTML entry file, create an index.html file, and write the following code:
Document const go = new Go (); / / definition WebAssembly.instantiateStreaming (fetch ('. / main-tiny.wasm'), go.importObject) in wasm_exec.js. Then (res = > {go.run (res.instance); / / execute the go main method})
Finally, set up a http server and let it run.
/ / python$ python3-m http.server$ python2-m SimpleHTTPServer// node$ npm I-g http-server$ hs// gp$ go get-u github.com/shurcooL/goexec$ goexec 'http.ListenAndServe (`: 8080`, http.FileServer (http.Dir (`.`)' exception record
Through the http-server service of node, an error will be reported when loading:
> TypeError: Failed to execute 'compile' on' WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
The reason is that the content-type of the wasm file response header is application/wasm; charset=utf-8 and should be application/wasm. The known workaround is to modify the initialization of wasm in HTML as follows:
Fetch ('. / main-tiny.wasm') .then (res = > res.arrayBuffer ()) .then (buffer = > {WebAssembly.instantiate (buffer, go.importObject) .then (res = > {go.run (res.instance);})}) the above is how the wasm environment is built in the golang language. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.