tinygo README

The project's README from github.com/tinygo-org/tinygo. As one markdown file: README.md.

TinyGo - Go compiler for small places

Linux macOS Windows Docker Nix

TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (wasm/wasi), and command-line tools.

It reuses libraries used by the Go language tools alongside LLVM to provide an alternative way to compile programs written in the Go programming language.

[!IMPORTANT] You can help TinyGo with a financial contribution using OpenCollective. Please see https://opencollective.com/tinygo for more information. Thank you!

Embedded

Here is an example program that blinks the built-in LED when run directly on any supported board with onboard LED:

package main

import (
    "machine"
    "time"
)

func main() {
    led := machine.LED
    led.Configure(machine.PinConfig{Mode: machine.PinOutput})
    for {
        led.Low()
        time.Sleep(time.Millisecond * 1000)

        led.High()
        time.Sleep(time.Millisecond * 1000)
    }
}

The above program can be compiled and run without modification on an Arduino Uno, an Adafruit Circuit Playground Express, a Seeed Studio XIAO-ESP32S3 or any of the many supported boards that have a built-in LED, just by setting the correct TinyGo compiler target. For example, this compiles and flashes an Arduino Uno:

tinygo flash -target arduino-uno examples/blinky1

WebAssembly

TinyGo is very useful for compiling programs both for use in browsers (WASM) as well as for use on servers and other edge devices (WASI).

TinyGo programs can run in Fastly Compute, Fermyon Spin, wazero and many other WebAssembly runtimes.

Here is a small TinyGo program for use by a WASI host application:

package main

//go:wasmexport add
func add(x, y uint32) uint32 {
	return x + y
}

This compiles the above TinyGo program for use on any WASI Preview 1 runtime:

tinygo build -buildmode=c-shared -o add.wasm -target=wasip1 add.go

You can also use the same syntax as Go 1.24+:

GOOS=wasip1 GOARCH=wasm tinygo build -buildmode=c-shared -o add.wasm add.go

Installation

See the getting started instructions for information on how to install TinyGo, as well as how to run the TinyGo compiler using our Docker container.

Supported targets

Embedded

You can compile TinyGo programs for over 150 different microcontroller boards.

For more information, please see https://tinygo.org/docs/reference/microcontrollers/

WebAssembly

TinyGo programs can be compiled for both WASM and WASI targets.

For more information, see https://tinygo.org/docs/guides/webassembly/

Operating Systems

You can also compile programs for Linux, macOS, and Windows targets.

For more information:

Currently supported features:

For a description of currently supported Go language features, please see https://tinygo.org/lang-support/.

Documentation

Documentation is located on our web site at https://tinygo.org/.

You can find the web site code at https://github.com/tinygo-org/tinygo-site.

Getting help

If you're looking for a more interactive way to discuss TinyGo usage or development, we have a #TinyGo channel on the Gophers Slack.

If you need an invitation for the Gophers Slack, you can generate one here which should arrive fairly quickly (under 1 min): https://invite.slack.golangbridge.org

Contributing

Your contributions are welcome!

Please take a look at our Contributing page on our web site for details.

Project Scope

Goals:

Non-goals:

Why this project exists

We never expected Go to be an embedded language, and so it’s got serious problems...

-- Rob Pike, GopherCon 2014 Opening Keynote

TinyGo is a project to bring Go to microcontrollers and small systems with a single processor core. It is similar to emgo but a major difference is that we want to keep the Go memory model (which implies garbage collection of some sort). Another difference is that TinyGo uses LLVM internally instead of emitting C, which hopefully leads to smaller and more efficient code and certainly leads to more flexibility.

The original reasoning was: if Python can run on microcontrollers, then certainly Go should be able to run on even lower level micros.

License

This project is licensed under the BSD 3-clause license, just like the Go project itself.

Some code has been copied from the LLVM project and is therefore licensed under a variant of the Apache 2.0 license. This has been clearly indicated in the header of these files.

Some code has been copied and/or ported from Paul Stoffregen's Teensy libraries and is therefore licensed under PJRC's license. This has been clearly indicated in the header of these files.

tinygo (tinygo-org/tinygo)

Searching Database…

17.8K ·GitHub
这是干什么的?TinyGo 是一个面向小型场景的 Go 编译器替代方案,适用于微控制器、WebAssembly(wasm/wasi)以及命令行工具。它把 Go 标准工具链的库和 LLVM 结合起来编译 Go 代码。
这是不是一门新语言?我还能写普通的 Go 吗?你写的还是普通的 Go:TinyGo 复用标准 Go 工具链的库,只是把后端换成了 LLVM。它支持大部分标准库包,大多数 Go 代码无需修改就能直接编译。
怎么安装它,并把第一个程序烧录到开发板上?按照 TinyGo 官网上的入门说明操作,或者直接使用官方 Docker 容器运行编译器。对于嵌入式目标,只需设置好编译器的 target,就能编译并直接烧录到开发板上。
随便给我一个 Go 程序它都能编译吗?不能。编译所有 Go 程序并不是它的目标,TinyGo 也不追求达到 gc 编译器的速度,或在海量 goroutine 场景下高效运行,不过涉及数值计算时 LLVM 可能反而更快。
它能编译到哪些平台——只有微控制器吗?支持 150 多种微控制器开发板,包括 Arduino Uno、Adafruit Circuit Playground Express 和 Seeed Studio XIAO-ESP32S3。它还能编译到浏览器里的 WASM、服务端的 WASI,以及 Linux、macOS 和 Windows。
同一个程序不改代码就能在不同开发板上运行吗?通常可以。README 里的板载 LED 闪烁示例不加修改就能在 Arduino Uno、Adafruit Circuit Playground Express 和 Seeed Studio XIAO-ESP32S3 上编译运行——你只需要改一下编译器的 target。
作者当初为什么要做 TinyGo?Rob Pike 说过,Go 从一开始就没打算做成嵌入式语言。他的思路是:既然 Python 能跑在微控制器上,那 Go 应该也能跑在更底层的微控制器上。和 emgo 不同,TinyGo 保留了 Go 的内存模型(包括垃圾回收),内部使用 LLVM 而不是生成 C 代码。
TinyGo 编译出的 WebAssembly 到底能跑在哪里?能在很多运行时里跑:README 提到了用于服务器和边缘设备的 Fastly Compute、Fermyon Spin 和 wazero,还能通过纯 WASM 在浏览器中运行。它可以编译到 WASI Preview 1,也支持 Go 1.24+ 引入的较新的导出语法。
支持 CGo 吗?支持——良好的 CGo 支持是这个项目的明确目标之一,开销不会超过一次普通的函数调用。
Creator
tinygo-org
Size
14 MB
Stars
17.8K
Age
8y
Commits · 90 Days