Sunday, November 09, 2025

GoLang => WASM => Vite plugin => JS

 vite-plugin-use-golang - npm

Write Go code in JavaScript files. It compiles to WebAssembly. Actually works.

You drop "use golang" at the top of a JS file, write Go code, and Vite compiles it to WASM at build time. The compiled functions show up on window like any other JavaScript function. It's absurd, but it's real.

// vite.config.js
import { defineConfig } from "vite";
import golangPlugin from "vite-plugin-use-golang";

export default defineConfig({
  plugins: [golangPlugin()],
});


Write Go in a JS file:

"use golang"

import (
  "fmt"
  "syscall/js"
)

func greet(this js.Value, args []js.Value) interface{} {
  return fmt.Sprintf("Hello from Go, %s!", args[0].String())
}

func main() {
  js.Global().Set("greet", js.FuncOf(greet))
  select {}
}


Use it in JavaScript:
console.log(window.greet("world")); // "Hello from Go, world!"