Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate a wasm file using golang and execute it on a browser

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use golang to generate wasm files and execute them on the browser". The content in the article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to use golang to generate wasm files and execute them on the browser".

What is webassembly?

Webassembly is a binary format that can be supported in web browsers or V8 environments. For more information, you can check this answer.

Start

Need to upgrade go to version 1.11 first

Write go files that need to be compiled into wasm files

/ / main.go

Package main

Func main () {

Println ("Hello, WebAssembly!")

}

Execute the build command

GOARCH=wasm GOOS=js go build-o test.wasm main.go

Note that this command is executed under the mac or linux operating system. Under windows, the environment variable should be set and then the compile command should be executed.

$env:GOARCH= "wasm"; $env:GOOS= "js"

Go build-o test.wasm main.go

After the command is executed, the test.wasm file is generated, which is the binary file that can be run on the browser.

Add additional dependencies

Copy the wasm_exec.html and wasm_exec.js files under $(go env GOROOT) / misc/wasm/ to the current directory

Set up web server

/ / test.go

Package main

Import (

"flag"

"log"

"net/http"

"strings"

)

Var (

Listen = flag.String ("listen", ": 8080", "listen address")

Dir = flag.String ("dir", ".", "directory to serve")

)

Func main () {

Flag.Parse ()

Log.Printf ("listening on% Q...", * listen)

Log.Fatal (http.ListenAndServe (* listen, http.HandlerFunc (func (resp http.ResponseWriter, req * http.Request) {

If strings.HasSuffix (req.URL.Path, ".wasm") {

Resp.Header (. Set ("content-type", "application/wasm")

}

Http.FileServer (http.Dir (* dir)) .ServeHTTP (resp, req)

})

}

Run the web service

Go run test.go

test

Open http://localhost:8080/wasm_exec.html in the browser and click the run button on the page to see the console printing Hello, WebAssembly!

So we can use go to write a program that can run in the browser.

How to use js

Using go's js library syscall/js

/ / main.go

Package main

Import "syscall/js"

Func sum (args [] js.Value) {

Var sum int

For _, val: = range args {

Sum + = val.Int ()

}

Println (sum)

}

Func registerCallbacks () {

Js.Global () .Set ("sum", js.NewCallback (sum))

}

Func main () {

C: = make (chan struct {}, 0)

Println ("Hello, WebAssembly!")

RegisterCallbacks ()

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report