300 points•iaaan•1 day ago•49 comments•

49 comments

neomantraabout 17 hours ago
I was a drive-by contributor to TinyGo, focused on getting MacOS and JS/WASM working well this summer, along with getting BubbleTea to work with it (the last of which was finally merged yesterday!). It was a lot of work, but now it unlocked so much capability for me.

The TinyGo project is an intensely talented cadre of developers. They care deeply this alternative Go compiler/runtime. It was really neat to see them in action and how an already established project accelerated in a year.

It was interesting to see how they adopted LLMs (basically use them but you better know what's going on) and how they dealt with newcomers. The project's scaffolding has improved to keep pace.

While it was intended to get Golang on small devices, the same love makes it work well for WASM. Also it improves as LLVM improves.

I rather consistently get 50% size binaries that run 10% faster and use lower memory than Vanilla Go. The drawback is 5x slower build times. So I dev-loop with Vanilla go and then prod-loop with TinyGo.

Booba will help you embed your Golang in a browser, either Vanilla or TinyGo. There's still some go.mod friction, but the next BubbleTea release will fix that.

https://nimbleterminal.com/booba

Here's some TinyGo interactive graphics next to Vanilla:

https://nimblemarkets.github.io/ntcharts3d/?demo=gallery&com...

Pro-tip: use gc=boehm for JS/WASM targets

noselasdabout 17 hours ago
Out of curiosity, what can and can't TinyGo do that the official Go compiler/runtime can do ?
bloppeabout 12 hours ago
Standard Go requires a pretty big runtime, mainly due to the GC, scheduler, and reflection. TinyGo uses much simpler versions of each in its runtime, and there are some subtle behavior differences. For instance, it uses cooperative scheduling for goroutines, rather than preemptive.
justincliftabout 16 hours ago
It compiles to much smaller sized executables, which in some environments is very important. ie webassembly, embedded devices
wkjagtabout 16 hours ago
The goal is to run on very limited resources, like micro controllers (ie ESP32, Arduino)
theplumberabout 15 hours ago
does WASM actually work in browser?

I know that in the standard Go compiler it is still “experimental”. What I mean by that is the program can hang due various issues with wind/unwind browser behaviour. The memory management seems to be another issue. You just can’t ship go-> wasm app in browser in production.

My hope is that once wasi 3 and a wasm-gc implementation will be released for Go then I will finally be able to actually use Go in the browser(via WASM). Until then it’s more or less a kind of java applets experience

aaron42netabout 14 hours ago
I used tinygo v0.4.1's WASM support in a multi-player browser game (https://smiloid.net) where I wanted to run the world simulation and drone logic to be shared between the browser and the server. In single-player, everything runs in the browser via WASM. In multi-player, the server maintains the authoritative copy and the client renders it predictively based on its user's inputs (modern game netcode style). The rendering engine is in the tinygo WASM, too, which dumps out trapezoids, circles, etc for the browser to draw.

To get the size down, I avoided including anything using reflection (including encoding/json and fmt) and left most of the runtime out, which means the WASM blob can't do concurrency or its own scheduling. The WASM blob is used basically as a set of function calls that take some input and return some output via a preallocated byte buffer shared between TypeScript and Go, with all of the timing, etc driven by the TS side.

This was a weird set of design tradeoffs, but the result is that thousands of lines of Go turn into 276k uncompressed (128k compressed) WASM blob that has 118k of minified JS (37k compressed) driving it that works pretty well. And I still got to make heavy use of Go's great fuzzing tools to chase down weird bugs in the drone logic.

Compared to "hello world" in normal Go being a few megabytes, this seemed pretty impressive, but you could certainly use more of the Go runtime and standard library if you weren't trying to optimize size as much.

stevekempabout 12 hours ago
It kinda depends on what you're doing.

I wrote a CP/M emulator, and only had to make trivial changes to compile it to WASM and run it in a browser with xterm.js. (Mostly relating to abstracting the filesystem, and adding a plugin for console I/O.)

No threading is involved, and nothing complex, but it does allow you to run turbo pascal, zork, etc, etc.

KuhlMenschabout 4 hours ago
Such a perfectly orienting top comment

taa! :trophy:

donatjabout 19 hours ago
Very nice! I have really been enjoying TinyGo for a couple Pi Pico projects I've been toying with.

Way saner than the C pico projects I've helped my friend work on.

Maybe it's just me but I kind of wish there was more of a way to clearly differentiate a Go project from a Tiny Go project, like .tgo file extensions or like a "compiler tinygo" directive in go.mod or something.

zanderwohlabout 12 hours ago
C on embedded is truly painful, not for language reasons, but for tooling reasons. I have never cloned a microcontroller project and built it with the directions in the README. There is always so much tinkering to do because the toolchain is so fragile that the slightest differences in your system cause the project to fail to compile.
donatjabout 12 hours ago
I've got my friend's projects compiling in a Docker container, so that definitely helps
gen2brainabout 19 hours ago
That is usually done with the build tags, for different OSes and arches, and TinyGo defines the tinygo build tag.
worgabout 14 hours ago
^ this, and if you really really wanted to have visual differentiation within files nothing prevents you to combine the build tag with a "compound extension" like `.tiny.go`
K0INabout 17 hours ago
Sorry, I did not use tiny go yet (it's on my bucket list for a long time), i thought it's compatible with go, why would you need a differentiation?
onlyrealcuzzoabout 16 hours ago
This seems pretty cool, but I can't gather from the README what it does and does not do:

``` Non-goals:

* Be efficient while using zillions of goroutines. However, good goroutine support is certainly a goal.

* Be as fast as gc. However, LLVM will probably be better at optimizing certain things so TinyGo might actually turn out to be faster for number crunching.

* Be able to compile every Go program out there.

```

This could mean almost literally anything from - practically - it doesn't compile any existing commonly used Go packages, and is worse at concurrency than Java to it will compile almost anything except packages that are doing really weird, generally accepted terrible things and outside of rare p999 edge cases, it's competitive with Go.

rob74about 15 hours ago
The "non-goals" section probably makes more sense if you read the "goals" section first, which boils down to "very small binary sizes, support for µcontrollers and WebAssembly, CGo support, be able to compile most Go code without modifications". The "non-goals" then basically say that they have to make some compromises to achieve the goals.
onlyrealcuzzoabout 14 hours ago
No - it still doesn't.

I get what it wants to do.

I have no idea what that costs.

mkarlinerabout 16 hours ago
I've working on MicroPython with my project thingstudio.net (sorry for plug) and the biggest issue I've had is memory fragmentation, especially on ESP32's. Does anyone have an insight/comparison on Go garbage collection vs Python?
shit_gameabout 22 hours ago
TinyGo has been my favorite means of using my Raspi Picos so far. My only gripe so far is that maps are nigh unusable due to their slowness, but this is generally simple enough to work around (at the expense of performance). I'm sure there are still more rough edges, but I don't think I'm experienced enough with embedded software to identify them.

Read the full thread on Hacker News →

Related stories