1. Home
  2. /
  3. Go-wasm
  4. /
  5. Go WASM Dynamic List

Go WASM Dynamic List - Go-wasm Typing CST Test

Loading…

Go WASM Dynamic List — Go-wasm Code

Adds items dynamically to a list when a button is clicked.

# go/demo/dynamic_list.go
package main

import (
	"syscall/js"
	"strconv"
)

func main() {
	doc := js.Global().Get("document")
	ul := doc.Call("createElement", "ul")
	doc.Get("body").Call("appendChild", ul)

	btn := doc.Call("createElement", "button")
	btn.Set("innerHTML", "Add Item")
	doc.Get("body").Call("appendChild", btn)

	count := 1
	btn.Call("addEventListener", "click", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
		li := doc.Call("createElement", "li")
		li.Set("innerHTML", "Item "+strconv.Itoa(count))
		ul.Call("appendChild", li)
		count++
		return nil
	}))

	select{}
}

Go-wasm Language Guide

Go-WASM refers to compiling Go (Golang) programs to WebAssembly, allowing Go code to run in the browser. It enables developers to leverage Go's concurrency model and standard library on the client-side, interacting with JavaScript and the DOM.

Primary Use Cases

  • ▸Porting existing Go libraries to run in the browser
  • ▸Computational-heavy browser tasks (e.g., data processing, simulations)
  • ▸SPAs with Go backend logic mirrored on the client
  • ▸Browser games leveraging Go routines
  • ▸Replacing JavaScript for Go-centric full-stack applications

Notable Features

  • ▸Compile Go code to WebAssembly for browser execution
  • ▸Interoperability with JavaScript using `syscall/js`
  • ▸Supports Go goroutines and channels (with some browser runtime constraints)
  • ▸Leverages Go's standard library in the browser
  • ▸Works with modern browsers supporting WebAssembly

Origin & Creator

Go-WASM is maintained by the Go project team at Google, introduced as part of Go 1.11 with WebAssembly as a supported compilation target.

Industrial Note

Go-WASM is ideal for Go developers who want to run Go code in the browser without rewriting logic in JavaScript or TypeScript, especially for apps needing Go's concurrency or existing Go code reuse.

More Go-wasm Typing Exercises

Simple Go WebAssembly ProgramGo WASM Button ClickGo WASM Input BindingGo WASM Conditional RenderingGo WASM Loop RenderingGo WASM Timer UpdateGo WASM Event CallbackGo WASM Fetch ExampleGo WASM Toggle Visibility

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher