Testing go 1.5 cross compilation on raspberry pi
I'm so excited with the new release of golang. One particular feature is now very easy to build for multiple architecture. If you seen my other posts, I also like to tinker with my raspberry-pi. On my previous project I use either ruby or python for building some stuff. One annoying thing is dependency, setup and compilation is usually quite slow. Would be cool if I could just create some stuff in desktop and just scp the binary to pi and everything should work!
There is this nice article that explain the process. Let's start there and create simple application
Let's try to build it
Now, crossing my finger, and run it on my pi
Holy crap, it's that easy. I'm excited. Let's see if go routine works
Again, build and scp, and cross some more fingers
Super awesome indeed, it just works!. Oh and the binary is not that big 1.9M considering it statically include the library.
Hmm let's try something fun that fiddle with the gpio pin. Let's create something that what raspberry-pi made for.. blinking led :p
There is already library. Let's starts there.
Dayuumm.. It's that easy. No more pip install or bundle install on pi. Just scp the binary!
Of course we've got this far, we must try to control it remotely via HTTP.
Look at that. All it takes to do that is just 67 lines of code. The main logic just about 31 lines. That's including the fancy dancing blinking. No framework, only gpio dependency. Everything else is go stdlib.
Ok, let's do one other thing. The reverse! Trigger something when button is pressed. For this let's just use websocket, and see how hard it is to implement this. What this mean that if you have websocket client (web browser) you could listen to event and stream it directly from your raspberry-pi to your computer.
You see there I connect 2 client. One browser and the other one is cli app that I created. Notice that they got their messages alternately between each other. This is because they are sharing the same channel. I will leave it to you my kind reader as an exercise to make it broadcast the message instead of distributing them :)
all source are available at https://github.com/yulrizka/go-pi-experiments
Site related post: