Learn Go-wasm - 10 Code Examples & CST Typing Practice Test
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.
Learn GO-WASM with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Simple Go WebAssembly Program
# go/demo/main.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
doc.Call("write", "Hello, Go WASM!")
select{}
}
A basic Go program compiled to WebAssembly that writes 'Hello, Go WASM!' to the browser document.
Go WASM Button Click
# go/demo/button.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
button := doc.Call("createElement", "button")
button.Set("innerHTML", "Click Me")
doc.Get("body").Call("appendChild", button)
text := doc.Call("createElement", "p")
doc.Get("body").Call("appendChild", text)
button.Call("addEventListener", "click", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
text.Set("innerHTML", "Button clicked!")
return nil
}))
select{}
}
Adds a button to the page that updates text when clicked.
Go WASM Input Binding
# go/demo/input.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
input := doc.Call("createElement", "input")
doc.Get("body").Call("appendChild", input)
text := doc.Call("createElement", "p")
doc.Get("body").Call("appendChild", text)
input.Call("addEventListener", "input", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
text.Set("innerHTML", input.Get("value"))
return nil
}))
select{}
}
Reads value from an input field and displays it.
Go WASM Conditional Rendering
# go/demo/conditional.go
package main
import (
"syscall/js"
)
func main() {
show := true
doc := js.Global().Get("document")
text := doc.Call("createElement", "p")
doc.Get("body").Call("appendChild", text)
if show {
text.Set("innerHTML", "Condition is true")
} else {
text.Set("innerHTML", "Condition is false")
}
select{}
}
Shows different messages based on a variable.
Go WASM Loop Rendering
# go/demo/loop.go
package main
import (
"syscall/js"
"strconv"
)
func main() {
doc := js.Global().Get("document")
for i := 1; i <= 5; i++ {
p := doc.Call("createElement", "p")
p.Set("innerHTML", "Item "+strconv.Itoa(i))
doc.Get("body").Call("appendChild", p)
}
select{}
}
Creates a list of items using a loop.
Go WASM Timer Update
# go/demo/timer.go
package main
import (
"syscall/js"
"time"
)
func main() {
doc := js.Global().Get("document")
text := doc.Call("createElement", "p")
doc.Get("body").Call("appendChild", text)
ticker := time.NewTicker(time.Second)
go func() {
for t := range ticker.C {
text.Set("innerHTML", t.Format("15:04:05"))
}
}()
select{}
}
Updates the current time every second.
Go WASM Event Callback
# go/demo/event.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
btn := doc.Call("createElement", "button")
btn.Set("innerHTML", "Click Event")
doc.Get("body").Call("appendChild", btn)
btn.Call("addEventListener", "click", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
js.Global().Get("console").Call("log", "Button clicked!")
return nil
}))
select{}
}
Registers a generic event callback for clicks.
Go WASM Fetch Example
# go/demo/fetch.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
p := doc.Call("createElement", "p")
doc.Get("body").Call("appendChild", p)
js.Global().Call("fetch", "https://api.github.com")
.Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
response := args[0]
response.Call("text").Call("then", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
p.Set("innerHTML", args[0].String())
return nil
}))
return nil
}))
select{}
}
Uses fetch to get data from an API and displays it.
Go WASM Toggle Visibility
# go/demo/toggle.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
p := doc.Call("createElement", "p")
p.Set("innerHTML", "Visible text")
doc.Get("body").Call("appendChild", p)
btn := doc.Call("createElement", "button")
btn.Set("innerHTML", "Toggle")
doc.Get("body").Call("appendChild", btn)
visible := true
btn.Call("addEventListener", "click", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
visible = !visible
if visible {
p.Set("style.display", "block")
} else {
p.Set("style.display", "none")
}
return nil
}))
select{}
}
Toggles the visibility of a paragraph when a button is clicked.
Go WASM Dynamic List
# 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{}
}
Adds items dynamically to a list when a button is clicked.
Frequently Asked Questions about Go-wasm
What is Go-wasm?
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.
What are the primary use cases for Go-wasm?
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
What are the strengths of Go-wasm?
Write browser logic in Go, reusing existing code. Strong typing and compile-time checks via Go compiler. Goroutines allow asynchronous/concurrent operations. Standard library available for many common tasks. Cross-platform: same Go code runs server and client (via WASM)
What are the limitations of Go-wasm?
Binary size can be large for simple apps. Performance overhead compared to native JavaScript in DOM-heavy operations. Debugging WASM can be challenging. Limited ecosystem of Go UI frameworks for browser. Goroutines are cooperative and may behave differently in WASM
How can I practice Go-wasm typing speed?
CodeSpeedTest offers 10+ real Go-wasm code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.