Hacker News

Show HN: Write Go code in JavaScript files

63 points by yar-kravtsov ago | 22 comments
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 |next [-]

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

liampulles |next |previous [-]

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.

zikani_03 |next |previous [-]

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.

foreigner |next |previous [-]

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

b_e_n_t_o_n |next |previous [-]

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.

h33t-l4x0r |next |previous [-]

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

foofoo12 |root |parent [-]

How big is imagemagick wasm?

hshdhdhehd |next |previous [-]

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

montakaoh |next |previous [-]

we need to go deeper

pjmlp |next |previous [-]

Cool hack, just use JavaScript.

Cthulhu_ |root |parent |next [-]

99 times out of a hundred, sure. But sometimes you need better performance or a library that isn't available in JS.

hollowturtle |root |parent |next [-]

Better performance? For javascript code that calls into native platform apis provided by the browser it's been alteady proven that performance is an order of magnitude better than calling into wasm and doing all the sheningans to move bytes from and to wasm

pjmlp |root |parent |previous [-]

WebGPU or WebGL is the answer.

kitd |root |parent |previous [-]

The author explains why you might want to use Go instead at the end of the readme.

onion2k |root |parent [-]

I don't think any of the use cases suggested really make sense though. For a compute-intense task like audio or video processing, or for scientific computing where there's likely to be a requirement to fetch a ton of data, the browser is the wrong place to do that work. Build a frontend and make an API that runs on a server somewhere.

As for cryptography, trusting that the WASM build of your preferred library hasn't introduced any problems demonstrates a level of risk tolerance that far exceeds what most people working in cryptography would accept. Besides, browsers have quite good cryptographic APIs built in. :)

tgv |root |parent |next [-]

> For a compute-intense task

The browser often runs on an immensely powerful computer. It's a waste to use that power only for a dumb terminal. As a matter of fact, my laptop is 6 years old by now, and considerably faster than the VPS on which our backend runs.

I let the browser do things such as data summarizing/charting, and image convolution (in Javascript!). I'm also considering harnassing it for video pre-processing.

pjmlp |root |parent [-]

You can take advantage of that power via WebGPU, or WebGL if the browser is not yet up to it.

preommr |root |parent |previous [-]

> For a compute-intense task like audio or video processing, or for scientific computing where there's likely to be a requirement to fetch a ton of data, the browser is the wrong place to do that work.

... I mean... elaborate?

Everytime I've heard somebody say this, it's always a form of someone stuck in the 90s/00s where they have this notion that browsers showing gifs is the ceiling and that real work can only happen on the server.

Idk how common this is now, but a a few years ago (~2017) people would show projects like figma tha drew a few hundred things on screen and people would be amazed. Which is crazy, because things like webgl, wasm, webrtc, webaudio are insanely powerful apis that give pretty low level access. A somewhat related idea are people that keep clamoring for dom access in wasm because, again, people have this idea that web = webpage/dom, but that's a segway into a whole other thing.

smashah |next |previous [-]

funny but this is going to become extremely popular.

nsonha |previous [-]

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

yar-kravtsov |root |parent |next [-]

Fair take! Though, this was literally built as a joke in response to @ibuildthecloud's tweet. Sometimes the dumbest ideas are the most fun to prototype.

iamsaitam |root |parent |previous [-]

Are there vaccines for these infected? I hope we can stop the spread /s