Either git clone the tinygo source from.
Navigate to your project and type:
go mod init
Replace each std package you need using the replace keyword in the go.mod file.
So if you want the machine package to be resolved replace it using.
replace machine => /path/to/tinygo/machine
Repeat this step for every package, which needs to be resolved in your project.
module github.com/Nerzal/tinygo-playground
go 1.14
replace machine => /home/tobias/go/src/github.com/tinygo-org/tinygo/src/machine
require (
machine v0.0.0-00010101000000-000000000000
tinygo.org/x/drivers v0.13.0
)
In order to get your package resolved, you also have to add a go mod file for the packages.
So navigate to /path/to/tinygo/src/neededpackage
And then do go mod init
cd ~/go/src/github.com/tinygo-org/tinygo/src/machine
go mod init
In order to get IDE support like autocomplete you have to add the tinygo src path to your GOPATH. So setup your Gopath first.
After adding the tinygo source path to the GOPATH go will know, where to look for the packages like machine
echo $GOPATH
If you result is empty, you need to set the GOPATH fist
You can just append the path to your tinygo installation in your GOPATH.
export GOPATH=$GOPATH:/path/to/your/tinygo
Windows uses a semicolon to separate the different paths. So you can just append the path to your tinygo using the example below.
set GOPATH=%GOPATH%;C:\path\to\your\tinygo
At this point, if you are in src/examples/bliny1/blinky.go, machine.Output can be handled by gopls. You may want to prompt them to check it once.
At this point, machine.LED cannot be processed by gopls. This is because you do not have build-tag or other settings in place.
The last step needed to get full code completion support is to start your editor with environment variables.
You could also just set the environment variables for your complete environment, but this would interfere you when, writing normal go code.
So we use another way.
We need different environment variables for different microcontrollers.
You can use tinygo info controllerName
Example To gather information needed to work with an Arduino use:
tinygo info arduino
$ tinygo info arduino
LLVM triple: avr-unknown-unknown
GOOS: linux
GOARCH: arm
build tags: avr baremetal linux arm atmega328p atmega avr5 arduino tinygo gc.conservative scheduler.none
garbage collector: conservative
scheduler: none
So now you now, that.
-tags=avr,baremetal,linux,arm,atmega328p,atmega,avr5,arduino,tinygo,gc.conservative,scheduler.none
Important is that you need to comma separate the tags.
The following example should work for all editors. And more or less all operating systems. The syntax may vary depending on your os/shell.
VSCode Example:
export GOOS=linux; export GOARCH=arm; export GOFLAGS=-tags=avr,baremetal,linux,arm,atmega328p,atmega,avr5,arduino,tinygo,gc.conservative,scheduler.none; code
There is a CLI tool called tinygo-edit you can use it to Gather the needed build flags and starting the editor using the correct environment variables. Using the CLI you don’t need to do the steps manualy.
Another alternative to automate the process of fetching the needed environment variables and starting your editor, you could also create an alias.
export VISUAL=code
export EDITOR="$VISUAL"
alias startTinyGoArduino="GOOS=linux GOARCH=arm GOFLAGS=-tags=$(tinygo info arduino|grep 'build tags'|awk -F: '{print $2}' | sed -e 's/^[[:space:]]*//'|sed -e 's/[[:space:]]/,/g') $EDITOR"
This process has only been tested with the gopls language server. It might or might not work with other language servers.
To install gopls follow the instructions in the link above.
There are already lot’s of drivers for common hardware. See this for more information.
go get tinygo.org/x/drivers