Vite Plugin Enables Writing Go Code in JavaScript Files That Compiles to WebAssembly
By
yar-kravtsov
7mo ago· 3 min readen
85/100
Golden Brown
Bagelometer↗
Front-window bakery material. Catches the eye, delivers the goods.
Score85Typehow-toSentimentpositive
Summary
A Vite plugin called 'vite-plugin-use-golang' enables developers to write Go code directly in JavaScript files, which then compiles to WebAssembly at build time. The compiled Go functions become available as JavaScript functions on the window object, allowing developers to leverage Go's standard libraries and performance in browser applications. The article demonstrates this with an example of image perceptual hashing, showing how Go's image library can replace complex JavaScript implementations.
Key quotes
· 4 pulledWrite 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.
Let's say you want to do image perceptual hashing in the browser. You could wrestle with Canvas APIs and write 200 lines of JavaScript. Or you could use Go's image standard library and let it handle the heavy lifting.
Write Go code in JavaScript files. Compiles to WebAssembly. Actually works.. Latest version: 0.1.1, last published: 4 months ago. Start using vite-plugin-use-golang in your project by running `npm i vite-plugin-use-golang`. There are no other projects in
