You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
IDEs need to have certain environment variables set before they work with TinyGo: `GOROOT` and `GOFLAGS`. You can determine the correct values from the `tinygo info` command (starting with TinyGo 0.15).
6
7
7
-
## Downloading the source
8
-
9
-
Either git clone the tinygo source from.
10
-
11
-
> https://github.com/tinygo-org/tinygo
12
-
13
-
## Go mod init in your project
14
-
15
-
Navigate to your project and type:
16
-
17
-
> go mod init
18
-
19
-
## Replace needed imports
20
-
21
-
Replace each std package you need using the replace keyword in the go.mod file.
22
-
23
-
So if you want the machine package to be resolved replace it using.
24
-
25
-
> replace machine => /path/to/tinygo/machine
26
-
27
-
Repeat this step for every package, which needs to be resolved in your project.
In order to get IDE support like autocomplete you have to add the tinygo src path to your [GOPATH](https://github.com/golang/go/wiki/GOPATH). So setup your Gopath first.
63
-
After adding the tinygo source path to the GOPATH go will know, where to look for the packages like `machine`
64
-
65
-
### Ubuntu Example
21
+
* The `GOROOT` value needed is the cached GOROOT given in the output (`/home/user/.cache/tinygo/goroot-go1.14-f930d5b5f36579e8cbd1c139012b3d702281417fb6bdf67303c4697195b9ef1f-syscall` in the example).
66
22
67
-
#### Check if GOPATH is set
23
+
* The `GOFLAGS` value can be determined from the build tags. Take the entire list of build tags, replace spaces with commas, and add `-tags=` in front. For this example, the output would be `-tags=cortexm,baremetal,linux,arm,nrf51822,nrf51,nrf,microbit,tinygo,gc.conservative,scheduler.tasks`.
68
24
69
-
> echo $GOPATH
25
+
Now you need to configure your IDE with these values.
70
26
71
-
If you result is empty, you need to set the GOPATH fist
27
+
### Visual Studio Code
72
28
73
-
You can just append the path to your tinygo installation in your GOPATH.
29
+
In VS Code, you can edit the file `.vscode/settings.json` in the root of your project. If the `.vscode` directory does not yet exist, create it. It's a normal JSON file where you need to set the `go.toolsEnvVars` property. An example file (again, using the above configuration) is the following:
74
30
75
-
> export GOPATH=$GOPATH:/path/to/your/tinygo
76
-
77
-
### Windows Example
78
-
79
-
Windows uses a semicolon to separate the different paths. So you can just append the path to your tinygo using the example below.
80
-
81
-
> set GOPATH=%GOPATH%;C:\path\to\your\tinygo
82
-
83
-
At this point, if you are in src/examples/bliny1/blinky.go, machine.Output can be handled by gopls.
84
-
You may want to prompt them to check it once.
85
-
86
-
At this point, machine.LED cannot be processed by gopls.
87
-
This is because you do not have build-tag or other settings in place.
88
-
89
-
## Starting your editor with variables
90
-
91
-
The last step needed to get full code completion support is to start your editor with environment variables.
92
-
93
-
You could also just set the environment variables for your complete environment, but this would interfere you when, writing normal go code.
94
-
95
-
So we use another way.
96
-
97
-
### Gather the needed information
98
-
99
-
We need different environment variables for different microcontrollers.
100
-
101
-
You can use `tinygo info controllerName`
102
-
103
-
**Example**
104
-
To gather information needed to work with an Arduino use:
105
-
106
-
> tinygo info arduino
107
-
108
-
```bash
109
-
$ tinygo info arduino
110
-
LLVM triple: avr-unknown-unknown
111
-
GOOS: linux
112
-
GOARCH: arm
113
-
build tags: avr baremetal linux arm atmega328p atmega avr5 arduino tinygo gc.conservative scheduler.none
3. GOFLAGS needs to be set to `-tags=avr,baremetal,linux,arm,atmega328p,atmega,avr5,arduino,tinygo,gc.conservative,scheduler.none`
123
-
124
-
Important is that you need to comma separate the tags.
40
+
After creating or modifying this file, you will likely need to restart VS Code to apply these settings.
125
41
126
-
### Start the editor
42
+
### Other IDEs
127
43
128
-
The following example should work for all editors. And more or less all operating systems. The syntax may vary depending on your os/shell.
44
+
Other IDEs will likely need a different setup. You can try starting them with these environment variables set in your shell or configuring these environment variables somewhere in your Go language server settings.
129
45
130
-
VSCode Example:
46
+
As an example, this is an alternative way to start VS Code with the settings above (using `bash`):
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"
148
66
```
149
67
150
-
### Note
68
+
##TinyGo Drivers
151
69
152
-
This process has only been tested with the [gopls](https://github.com/golang/tools/blob/master/gopls/doc/user.md) language server. It might or might not work with other language servers.
70
+
There are already lot's of drivers for common hardware. See [this](https://github.com/tinygo-org/drivers) for more information.
153
71
154
-
To install gopls follow the instructions in the link above.
72
+
### Using drivers with Go modules
155
73
156
-
## TinyGo Drivers
74
+
When you're using Go modules, drivers are automatically added when you import a driver (such as `tinygo.org/x/drivers/ws2812`).
157
75
158
-
There are already lot's of drivers for common hardware. See [this](https://github.com/tinygo-org/drivers) for more information.
76
+
### Install drivers using GOPATH
159
77
160
-
### Install drivers
78
+
When you're not using Go modules, you need to download the drivers separately. This works like any Go package:
0 commit comments