Greeting
Calls a Go function that formats a string.
Fibonacci
Computes Fibonacci(n) in Go using math/big — handles arbitrarily large numbers.
Reverse & Palindrome
Reverses a string and checks if it's a palindrome — Go handles Unicode runes correctly.
Go source compiled to WASM at server startup
//go:build js && wasm
package main
import (
"fmt"
"math/big"
"strings"
"syscall/js"
)
func greet(this js.Value, args []js.Value) interface{} {
name := args[0].String()
return fmt.Sprintf("Hello from Go, %s! Running as WebAssembly.", name)
}
func fibonacci(this js.Value, args []js.Value) interface{} {
n := args[0].Int()
if n <= 0 { return "0" }
if n == 1 { return "1" }
a, b := big.NewInt(0), big.NewInt(1)
for i := 1; i < n; i++ {
a.Add(a, b)
a, b = b, a
}
return b.String()
}
func reverseString(this js.Value, args []js.Value) interface{} {
runes := []rune(args[0].String())
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func isPalindrome(this js.Value, args []js.Value) interface{} {
s := strings.ToLower(strings.ReplaceAll(args[0].String(), " ", ""))
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
if runes[i] != runes[j] { return false }
}
return true
}
func main() {
js.Global().Set("goGreet", js.FuncOf(greet))
js.Global().Set("goFibonacci", js.FuncOf(fibonacci))
js.Global().Set("goReverse", js.FuncOf(reverseString))
js.Global().Set("goIsPalindrome",js.FuncOf(isPalindrome))
js.Global().Set("goReady", js.ValueOf(true))
<-make(chan struct{})
}