Show HN: Write Go code in JavaScript files

npmjs.com

96 points by yar-kravtsov 9 hours ago


I built a Vite plugin that lets you write Go code directly in .js files using a "use golang" directive. It compiles to WebAssembly automatically.

tkzed49 - 4 hours ago

Beautiful. Minor feedback: rather than having a "use golang" directive, just allow imports of .go files. This is more idiomatic for JS bundlers.

liampulles - 4 hours ago

Just be careful with this backend-code-in-frontend stuff. If it's needed for some computationally expensive logic that is logically client side, then fine. But be wary of letting the client dictate business rules and having open-for-anything APIs (GraphQL is particularly prone to this).

I've seen teams do this in the wild more than once.

foreigner - 6 hours ago

Reminds me of this toy I made some years ago: https://www.npmjs.com/package/polyglot-tag

zikani_03 - 4 hours ago

Looks interesting and good use case for introducing folks to extending web apps with WASM functionality.

Used a similar technique using tinygo wasm builds (without Vite ofcourse) on toy project where WASM based functionality acted as a fallback if the API wasn't available or user was offline - found it an interesting pattern.

lisbbb - an hour ago

I'm guessing this only works on back end? If yes, then why not just write the back end in Go if you're so fond of the language? It's not like Golang lacks the libraries to do web stuff. Would it be like some shop that is all React, Angular, or some other?

b_e_n_t_o_n - 6 hours ago

Hah. Back in the day I wrote a plugin to convert Lua files into a module that ran via one of the JS lua vms. Good fun.

pjmlp - 6 hours ago

Cool hack, just use JavaScript.

h33t-l4x0r - 5 hours ago

How big is it? Is it smaller than imagemagick wasm?

hshdhdhehd - 7 hours ago

Like it. Especially the how to use it and when to use it guidance.

montakaoh - 5 hours ago

we need to go deeper

smashah - 6 hours ago

funny but this is going to become extremely popular.

nsonha - 6 hours ago

seems like an unintuitive idea that could have only come from someone infected by react/vercel. The natural way that most would think about this is just write go in a go file and have an import attribute or macro