Hacker News new | past | comments | ask | show | jobs | submit login
Tinygo: LLVM-based Go compiler for microcontrollers, WASM, and CLI tools (github.com/tinygo-org)
182 points by gilad on June 6, 2022 | hide | past | favorite | 22 comments



Huge fan of Tinygo. I use it for the reduced WASM sizes. I see ~10x reduction when using it. I am working on a project that does some business logic/text parsing[0] that I want to be available on multiple platforms.

With TinyGo + wasmer[1] I can build a library that is usable in Python, PHP, Javascript and C++. It has been a lot of fun for me. Lots of rough edges still exist when moving between WASM/calling language, but when you get it working it feels amazing.

[0] https://github.com/pion/explainer

[1] https://wasmer.io/


Tinygo is neat, but the one time I tried to use it in practice, it failed me. The following program will panic when compiled to wasm:

  package main  
  import "encoding/json"  
  func main() {  
          var parsed interface{}  
          json.Unmarshal([]byte(`{"test":42}`), &parsed)  
  }
(It's been a while, so I forgot the problem with it was.)

I'm curious what kind of text parsing you do. I ended up rolling with valyala/fastjson, but that's very much not what I was imagining. Lots of existing libraries don't work either, and finding out what does and what doesn't is pain. Is there some trick to working with it?


I don't think "encoding/json" is fully working yet.

Seems to be importable now, although tests are not passing. See supported packages here: https://tinygo.org/docs/reference/lang-support/stdlib/

Definitely still a ways to go before the full stdlib is usable.

Looks like your code still fails at runtime with the error:

    panic: unimplemented: (reflect.Type).NumMethod()
Tested here: https://play.tinygo.org/


Dunno bro, it doesn't work on the 6502, so... ;)


By today standards, 6502 is a nanocontroller... or maybe even a picocontroller. Go buy yourself a real microcontroller, y'hear me? /s


I mean in a world where a 32-bit microcontroller with flash in the 1-2MB range and a few hundred KiB RAM can cost less than $2 why make things harder on yourself :P

If you're using a chip without the bells and whistles for precise timing reasons, then you're probably gonna program it in assembly too anyways


Tinygo is great. I think they could address a much larger "market" by improving support for ARM-based systems, notably iOS and Android. I've experimented with cross-compiling Golang code for these platforms and it works great with tools like gomobile, the binary size and runtime overhead can be prohibitive though. I looked at Tinygo to see if it could help with that but couldn't figure out how to compile code for Android, I think it would have been a perfect solution otherwise. In my understanding it supports most of the TLS stack and many other standard libraries as well, which makes it realistic to use it as a systems language on mobile platforms.

I know that Tailscale keeps their own fork of Golang with some optimizations for mobile as well, and where they try to merge stuff back into the mainline, so maybe in the future binary size and runtime overhead will go down for regular Golang. Tinygo seems to do a much better job at reducing that though, so it would be great to have it work on mobile.


Right now we don't have any support for Android and iOS in particular, but we do support Linux/ARM (regular Linux). It's an interesting use case however.

> In my understanding it supports most of the TLS stack and many other standard libraries as well

We don't have a proper net package yet so usage on Android/iOS will be rather limited.


Another advantage of tinygo is that unlike "Go proper" FFI with C code is blazingly fast, which makes it a reasonable option for inter-oping with C-library- whereas use of Cgo is actively discouraged in "Go proper" due to performance and other reasons. So using board specific libraries which are very likely to be in C is doable via tinygo.


Aside: the go team maintains frontends for gcc and llvm, though not targeting microcontrollers like this.

https://github.com/golang/gofrontend

https://go.googlesource.com/gollvm/


do either one of those have builds? I would like to try them out, but not enough to build myself


Oof - I clicked through thinking "oh, it can't be that bad", but that looks like it's doing an llvm build which can be a bear in terms of disk/CPU/RAM IIRC.


Gccgo is packaged by any reasonable Linux distribution.


I'm using Windows


Gccgo does not support windows.


I have been very interested in the Zig language for doing micro-controller stuff as I wanted a more modern version of C. But I am beginning to think Tinygo is a better alternative for me. No doubt Zig is a great language but I don't want to always be chasing obscure languages. Go has the benefit of being quite mainstream at this point with a decent size community.

Go is also extremely easy to learn and get going with.


Tinygo is honestly extremely niche. Similar to circuitpython or micropython I really wouldn't reccomend anything but hobby toys to be written in it.


Tinygo is supported by several edge computing platforms, and works well for at least one commercial application.

If you need wasm/wasi support, and you're ok with working around the partial support for reflect etc., it's not a bad choice.


The website: https://tinygo.org/


For Arduino-like microcontroller programming, board and device support are pretty important (much like an operating system). Which ones you care about depends on your application.

Looks like they have support listed for lots of boards [1] and lots of devices [2]. It won't work for me yet, though, because I need USB serial and MIDI, and they seem not to be implemented yet, at least for the Teensy and Raspberry Pi Pico.

[1] https://tinygo.org/docs/reference/microcontrollers/ [2] https://tinygo.org/docs/reference/devices/


Is Wi-Fi for ESP32 devices is working yet?

I didn't see anything specific one the supported boards page.


I was recently working on a project and bumped into this. It's not currently but here are the relevant Github links if you want to learn more (it doesn't look like there's much movement recently):

https://github.com/tinygo-org/tinygo/issues/1427

https://github.com/tinygo-org/drivers/pull/320




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: