The Go bindings allow you to control Bricks and Bricklets from your Go programs. The ZIP file for the bindings contains:
github.com/Tinkerforge/go-api-bindings
the source code of the bindingsexamples/
the examples for every Brick and BrickletThere are two ways to install the Go bindings: from GitHub or from source.
The bindings are available from the GitHub (in this case you don't
even need the ZIP file for the bindings). Install the bindings using the
go get
tool:
go get -u github.com/Tinkerforge/go-api-bindings
This does not include the examples. Those are available as part of the bindings ZIP file.
Extract the github.com
directory from the ZIP file to a directory of your choice.
Add the following line to the go.mod
file of your Go program, to tell the Go
compiler where to find the Go bindings:
replace github.com/Tinkerforge/go-api-bindings => /path/to/your/github.com/Tinkerforge/go-api-bindings
Replace the /path/to/your
part with the directory you extracted the
github.com
directory from the ZIP file to.
Now you're ready to test an example.
To test a Go example Brick Daemon and Brick Viewer have to be installed first. Brick Daemon acts as a proxy between the USB interface of the Bricks and the API bindings. Brick Viewer connects to Brick Daemon and helps to figure out basic information about the connected Bricks and Bricklets.
As an example let's compile the configuration example for the Stepper Brick
from the command line. For this we use the example_configuration.go
file from the examples/StepperBrick/
folder of the ZIP file.
In the example ADDR
specifies at which network address the
Stepper Brick can be found. If it is connected locally to USB then localhost:4223
is correct.
The UID
value has to be changed to the UID of the
connected Stepper Brick, which you can figure out using Brick Viewer:
const ADDR string = "localhost:4223"
const UID string = "XXYYZZ" // Change XXYYZZ to the UID of your Stepper Brick.
Now we can build and run the project with go run example_configuration.go
.
Links to the API reference for the IP Connection, Bricks and Bricklets as well as the examples from the ZIP file of the bindings are listed in the following table. Further project descriptions can be found in the Kits section.