Space Cat, Prince Among Thieves

Read Browser File Inputs with Go WebAssembly

I have been playing with WebAssembly since Go added support for it in 2017.

For a very long time now, I've been wanting to be able to accept files from an end user and process them with Go on their local machine. Basically I just want to read files from file inputs. I knew it had to be possible, but could not in all this time find a good walkthrough online. A couple nights ago I set my mind to it and figured it out.

Essentially we need to execute the following JavaScript, but in Go via the JS syscall API.

So after transliterating that JavaScript into Go syscall/js calls, we then using js.CopyBytesToGo to pass the given Uint8Array back to our WASM runtime where we can do with the bytes as we please.

Note, given the keyword any here, this example requires Go 1.18 - the syscall/js library is still considered experimental and expect it to break on major go releases.

This is as written for brevity and could be improved in code safety in a number of pretty obvious ways.

The iframe below runs the compiled WASM and spits out any file you upload to it, via Go.

Example


Email address will never be publicly visible.

Basic HTML allowed.