Learn TINYGO-WASM with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
Simple TinyGo WASM Program
# tinygo/demo/main.go
package main
import (
"syscall/js"
)
func main() {
doc := js.Global().Get("document")
doc.Call("write", "Hello, TinyGo!")
select{}
}
A basic TinyGo program compiled to WebAssembly that prints 'Hello, TinyGo!' when executed.
2
TinyGo WASM Counter
# tinygo/demo/counter.go
package main
import (
"syscall/js"
)
var count int
func increment(this js.Value, args []js.Value) interface{} {
count++
doc := js.Global().Get("document")
doc.Call("write", count)
return nil
}
func main() {
doc := js.Global().Get("document")
doc.Call("write", "Counter: ")
js.Global().Set("increment", js.FuncOf(increment))
select{}
}
A simple counter using TinyGo and WebAssembly.
3
TinyGo WASM Button Click
# tinygo/demo/button.go
package main
import (
"syscall/js"
)
func click(this js.Value, args []js.Value) interface{} {
doc := js.Global().Get("document")
doc.Call("write", "Button clicked!")
return nil
}
func main() {
js.Global().Set("clickButton", js.FuncOf(click))
select{}
}
Prints a message when a JavaScript button is clicked.
4
TinyGo WASM Hello with Input
# tinygo/demo/input.go
package main
import (
"syscall/js"
)
func greet(this js.Value, args []js.Value) interface{} {
name := args[0].String()
doc := js.Global().Get("document")
doc.Call("write", "Hello, "+name+"!")
return nil
}
func main() {
js.Global().Set("greet", js.FuncOf(greet))
select{}
}
Reads input value and prints a greeting.
5
TinyGo WASM Even Check
# tinygo/demo/even.go
package main
import (
"syscall/js"
)
func isEven(this js.Value, args []js.Value) interface{} {
n := args[0].Int()
result := "Odd"
if n%2 == 0 {
result = "Even"
}
doc := js.Global().Get("document")
doc.Call("write", result)
return nil
}
func main() {
js.Global().Set("isEven", js.FuncOf(isEven))
select{}
}
Checks if a number is even and prints the result.
6
TinyGo WASM Fibonacci
# tinygo/demo/fibonacci.go
package main
import (
"syscall/js"
)
func fib(n int) int {
if n <= 1 {
return n
}
return fib(n-1) + fib(n-2)
}
func fibonacci(this js.Value, args []js.Value) interface{} {
n := args[0].Int()
result := fib(n)
doc := js.Global().Get("document")
doc.Call("write", result)
return nil
}
func main() {
js.Global().Set("fibonacci", js.FuncOf(fibonacci))
select{}
}
Calculates Fibonacci number and prints it.
7
TinyGo WASM Toggle Text
# tinygo/demo/toggle.go
package main
import (
"syscall/js"
)
var on bool
func toggle(this js.Value, args []js.Value) interface{} {
on = !on
text := "OFF"
if on {
text = "ON"
}
doc := js.Global().Get("document")
doc.Call("write", text)
return nil
}
func main() {
js.Global().Set("toggle", js.FuncOf(toggle))
select{}
}
Toggles displayed text between 'ON' and 'OFF'.
8
TinyGo WASM Add Numbers
# tinygo/demo/add_numbers.go
package main
import (
"syscall/js"
)
func add(this js.Value, args []js.Value) interface{} {
a := args[0].Int()
b := args[1].Int()
doc := js.Global().Get("document")
doc.Call("write", a+b)
return nil
}
func main() {
js.Global().Set("add", js.FuncOf(add))
select{}
}
Adds two numbers passed from JavaScript and prints the sum.
9
TinyGo WASM Log Message
# tinygo/demo/log.go
package main
import (
"syscall/js"
"fmt"
)
func logMessage(this js.Value, args []js.Value) interface{} {
fmt.Println("Hello from TinyGo WASM!")
return nil
}
func main() {
js.Global().Set("logMessage", js.FuncOf(logMessage))
select{}
}
Logs a message to the console from WASM.
10
TinyGo WASM Multiply Numbers
# tinygo/demo/multiply.go
package main
import (
"syscall/js"
)
func multiply(this js.Value, args []js.Value) interface{} {
a := args[0].Int()
b := args[1].Int()
doc := js.Global().Get("document")
doc.Call("write", a*b)
return nil
}
func main() {
js.Global().Set("multiply", js.FuncOf(multiply))
select{}
}
Multiplies two numbers passed from JavaScript and prints the result.