gaupoit

Developer – Writer

How could I meet the cute animal (golang) ? — June 5, 2015

How could I meet the cute animal (golang) ?

Yoooo guys!!! Today is Friday which I love much in the week. I ended this Friday which some tasks still have not finished yet. What’s a boring day I thought!. I need to refresh my head by find something to do this weekend. I remembered that I just bought a Raspberry Pi 2 (RaspPI) toy last week and have never tried to made it fun before. Ok! Let’s make some fun with RaspPi :D. After googling I found an interesting framework which is http://gobot.io/ and it also supports my toy. I decide to go home and play with golang in RaspPI.

I. How could I install go in Raspberry PI 2?

First of all, we need to find the way to install go in ARM CPU Architecture  because my RaspPI’s heart made by this architecture. The plan for completing this task includes 4 steps : preparation, building, packing the compiler(optional), installation.

1.Preparation

Begin with making some build directory and cloning the golang repository.


mkdir ~/build

cd build

git clone https://github.com/golang/go.git

….. wait a minute guy. Time to break and go to toilet :D.

Oh yeah! It has already finished. Then we will choose the branch we want to check-out.


git branch -av
* master 54789ef cmd/internal/obj/arm64: make function prologue more predictable
remotes/origin/HEAD -> origin/master
remotes/origin/dev.cc a91c2e0 [dev.cc] cmd/internal/obj: set ctxt.Windows != 0 on windows
remotes/origin/dev.garbage db40624 [dev.garbage] runtime: raise StackGuard limit for Windows (again)
remotes/origin/dev.power64 7904e95 [dev.power64] liblink: fix Solaris build
remotes/origin/dev.ssa bd95412 [dev.ssa] cmd/compile/internal/ssa: add a String() method to Func
remotes/origin/master 54789ef cmd/internal/obj/arm64: make function prologue more predictable
remotes/origin/release-branch.go1 08b97d4 [release-branch.go1] doc: clarify that the displayed tar file name is an example
remotes/origin/release-branch.go1.1 1d6d8fc go1.1.2
remotes/origin/release-branch.go1.2 43d00b0 go1.2.2
remotes/origin/release-branch.go1.3 3dbc53a go1.3.3
remotes/origin/release-branch.go1.4 883bc6e [release-branch.go1.4] go1.4.2
remotes/origin/release-branch.r57 7998d01 [release-branch.r57] docs/GoCourseDay1.pdf: fix error in operator table.
remotes/origin/release-branch.r58 0584eb2 [release-branch.r58] reflect: disallow Interface method on Value obtained via unexported name
remotes/origin/release-branch.r59 5d97657 [release-branch.r59] gc: fix closure bug
remotes/origin/release-branch.r60 394b383 [release-branch.r60] doc: document release.r60.3

As we can see, origin/release-branch.go1.4 is the lasted release that’s reason why I will checkout this revision.


git checkout -b go1.4 origin/release-branch.go1.4

At that time, we already prepared everything we need. Being confidence to go to next steps.

2.  Building the compiler

Before building this animal, we have to force it to build it on ARMv7 (RaspBerry Pi 2) or ARMv6 (Raspberry Pi) CPU Architecture.


export GOARM=7

or


export GOARM=6

After that, we will move to src folder and run make.bash script.


cd src

./make.bash

Now waiting the magic happens and drinking beer. Eventually, you must see these line:


Installed Go for linux/arm in /home/pi/build/go
Installed commands in /home/pi/build/go/bin

Hehe, mission completed. I am careful guy so I want to packing the compiler I just complied.

3. Packing the compiler

I will build a tarball for this go compiler


cd ~/build
tar -czvf go-1.4.1-pi.tar.gz go/

We have own backup tarball. Fell free to install the compiler.

4.Install the compiler

Let’s move our go compiler from build to /user/local.


cd ~/build
sudo mv go /usr/local/
sudo chown -R root:root /usr/local/go
sudo chmod -R 755 /usr/local/go

After moving, let go to /user/local/go and continue to make a tarball.


sudo tar -C /usr/local -zxvf go-1.4.1-pi.tar.gz
sudo chown -R root:root /usr/local/go
sudo chmod -R 755 /usr/local/go

In addition, we need to add $PATH and $GOPATH in ~/.profile (for your user account) or /etc/profile (for entire OS) in the end of file the following lines.


export PATH=$PATH:/usr/local/go/bin

export GOPATH=$HOME/go

Cool! To make sure it will be effected. Let’s reboot our Raspberry Pi.

Let’s try some check go version to make sure we could finish all our steps.


go version
go version go1.4.1 linux/arm

Finally, I saw the golang animal in my Raspberry PI 2. The next mission is “How to install gobot to Raspberry PI 2?”

II. How could I install gobot to Raspberry PI 2?

At this point, we had Go compiler. Now we need to get the gobot repository and install it by coping and pasting this line:

<pre class="CodeRay">go get -d -u github.com/hybridgroup/gobot/... && go install github.com/hybridgroup/gobot/platforms/raspi

Next, we will go to gobot directory, build and run it.

cd $GOPATH/src/github.com/hybridgroup/gobot/examples/
go build raspi_blink.go
cd $GOPATH/src/github.com/hybridgroup/gobot/
./raspi_blink
2015/06/06 08:26:59 Initializing Robot blinkBot ...
2015/06/06 08:26:59 Initializing connections...
2015/06/06 08:26:59 Initializing connection raspi ...
2015/06/06 08:26:59 Initializing devices...
2015/06/06 08:26:59 Initializing device led ...
2015/06/06 08:26:59 Starting Robot blinkBot ...
2015/06/06 08:26:59 Starting connections...
2015/06/06 08:26:59 Starting connection raspi...
2015/06/06 08:26:59 Starting devices...
2015/06/06 08:26:59 Starting device led on pin 7...
2015/06/06 08:26:59 Starting work...

Ok, great! We clearly see some things “Starting…”. What the hack is that I wondered. Ahh I need some toys such as led, board, cable or something I don’t know. Let’s by them right now and continue our new journey “How could I control a led on/off with gobot in Raspberry PI 2?”