Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to quickly build single-page web application with WebAssembly + Vugu

Shulou Source: shulou.com Published: 2022-06-01 15:40:18 09月20日 Update

In this issue, the editor will bring you about WebAssembly + Vugu how to quickly build single-page web application. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Vugu?

Vugu is a Go Library that can easily write HTML, CSS, and Go, which can be compiled and run in a browser using WebAssembly to build your single-page web application (single-page web applications). The name Vugu actually comes from a combination of Vue and Go, and although it seems to be handled in a similar way to js writing Vue, officials are also looking for ways that are more suitable for Go. Hello, World Let's create a Vugu application that can run in a browser with only three files to prepare. But you need to make sure that the Go version is 1.13 or higher.

Create a folder and go.mod

Mkdir vuguTestApp & cd vuguTestApp & go mod init

Create a Vugu component file, root.vugu. Here we implement a click handler and a button to demonstrate some basic functions, from which you can see a little bit of Vue

Test WebAssembly is awesome!

.my-first-vugu-comp {background: # eee;}

Type RootData struct {Show bool} func (data * RootData) Toggle () {data.Show =! data.Show}

c. Create a file devserver.go that provides server. The contents of this file will not be compiled by WebAssembly, but just act as a server. You can notice the + build ignore in the code.

/ / + build ignorepackage main

Import ("log"net/http"os"

"github.com/vugu/vugu/simplehttp")

Func main () {wd, _: = os.Getwd () l: = "127.0.0.1 log.Printf 8844" log.Printf ("Starting HTTP Server at% Q", l) h: = simplehttp.New (wd, true) / / if you have a CSS file, you can quote / / simplehttp.DefaultStaticData ["CSSFiles"] = [] string {"/ my/file.css"} log.Fatal (http.ListenAndServe (l, h))}

d. Execute go run devserver.go

The service will be started, and the operation effect will be the same on Windows,Linux or Mac. After startup, vugu will help you generate a main_wasm.go file.

/ / + build wasm

Package main

Import ("log"os"

"github.com/vugu/vugu")

Func main () {

Println ("Entering main ()") defer println ("Exiting main ()")

RootInst, err: = vugu.New (& Root {}, nil) if err! = nil {log.Fatal (err)}

Env: = vugu.NewJSEnv ("# root_mount_parent", rootInst, vugu.RegisteredComponentTypes ()) env.DebugWriter = os.Stdout

For ok: = true; ok; ok = env.EventWait () {err = env.Render () if err! = nil {panic (err)}}

} / / files that will be compiled to the .wasm suffix (/ / + build wasm)

e. The browser opens http://127.0.0.1:8844/ and you can see the page.

This is how the WebAssembly + Vugu shared by the editor quickly builds single-page web application. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: File code content browser browse compile way analysis application service run similar three professional small and medium function rich content function just just Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology Linux Shulou Information NVidia