Webassembly error undefined: userCurrent

I am playing with wasm. But when compiling I get an error:

GOOS=js GOARCH=wasm go build -o main.wasm # github.com/lib/pq ../../github.com/lib/pq/conn.go:321:13: undefined: userCurrent

Any idea what this is and how I can solve this?

@Sibert I just checked github.com/lib/pq/conn.go line 321 and I don’t see any reference to userCurrent. Can you go get that package and then try again?

No difference. More suggestions?

$ GOOS=js GOARCH=wasm go build -o main.wasm  # github.com/lib/pq
../../github.com/lib/pq/conn.go:321:13: undefined: userCurrent

$ go get github.com/lib/pq

$ GOOS=js GOARCH=wasm go build -o main.wasm # github.com/lib/pq
../../github.com/lib/pq/conn.go:321:13: undefined: userCurrent

OK, If I git blame conn.go and go back to the commit that last changed line 321 (commit cd7087504b4639a9b67e108f365062ab26335e8b), when I scroll up, I see that line 321 now includes a call to a userCurrent function. I recommend updating with the latest code from GitHub, because what you have seems to be a bit out of date somehow. Anyway, if I check where that function is defined, I see there’s a user_posix.go and user_windows.go. Neither of those build constraints apply to Wasm, so the function isn’t defined when building for Wasm. You’ll have to change lib/pq’s source code to either not require that userCurrent function, or to implement it yourself for wasm.

I comment out lib/pq (Do I need lib/pq if I am using sqlx?), then the go build worked. But I cannot start the executable on Mac. What am I doing wrong?

$ GOOS=js GOARCH=wasm go build -o main.wasm
$ ./main.wasm
-bash: ./main.wasm: cannot execute binary file

You cannot just run Wasm modules from the command line. Wasm modules run client-side in a web browser. You have to have a web page that loads your module into the browser with something like:
WebAssembly.instantiateStreaming(fetch('main.wasm'), importObject) // ... And then you can reference the functions exposed in your main.wasm module from Javascript running in the browser.

lib/pq is the Postgres SQL database driver. Without it, you cannot use a Postgres DB.

Why are you building a Wasm module if you just want to run it on your Mac? Why not just build for Mac?

Does not work. How do the browser know where the main.wasm is?

So it must be lib/pq is damaged or not updated?

I am investigation what this site

http://94.237.92.101:3030/mytopics

could look and feel when using Webassembly. But it seems not be production ready in Go?

You need your main.wasm file to be accessible to your web application just like other Javascript scripts.

lib/pq is almost certainly meant to be used server-side. Your browser should not be making direct connections to your back end database. The lib/pq developers have probably not implemented support for Wasm.

Last I heard, it’s not production ready, but only because the entire Go runtime needs to be compiled in, so Go Wasm modules are huge compared to C++ or C Wasm modules. To the best of my knowledge, it works and it’s stable, but it takes a lot longer to initialize than plain ol’ C.

I don’t think you should be running the code that talks to your DB inside of the browser. Even if you get it lib/pq to compile for wasm, I don’t know if it’ll actually work. I thought the Wasm environment was restricted from making raw socket connections by design, so I don’t know if you’ll even be able to connect to the DB.

1 Like

Thank you for your answers. It seems that I do not fully understand the concept with wasm.

I have interpreted that wasm is running like an app in your browser. But do I need a server as well? Which parts should be on the server and which parts in the browser?

There are few examples of wasm out there, but all I have tested takes long time to load, but acts more like a desktop.

Take a look at the Use Cases page here. Wasm seems to be about making CPU-intensive client-side code run more efficiently than it can when written in Javascript. If you’re making in-browser games, performing image recognition, VR, CAD, etc., it makes sense to use Wasm. I don’t believe its goal is to eliminate the need for a web server.

If I interpret this correct, a webserver is uploading the wasm to the client? How and where is the SQL stuff done? In the web server or in the browser?