Archives For Gocode

I spent a bit of time today getting my various Raspberry Pi boards up to snuff. I worked with one Raspberry Pi 2 Model B V1.1 (in the foreground) and my newer Raspberry Pi 3 Model B V1.2 (background). Both boards are back to using Arch Linux ARM, my comments about going off on a tangent with Raspbian not withstanding. I’m using the RPi 3 as a “build system”, transferring binaries to the RPi 2. Specifically I’m writing code with Google’s Go (golang). The compilation of Go code on the RPi 3 is quite fast. Test Go binaries are secure copied (scp) from the RPi3 to the RPi2 for testing.

I’d written earlier on this blog about trying to use Go on the older Raspberry Pi B+, back when Go was around version 1.4, and back when there were no installable packages for Arch Linux ARM. Because of Go’s lack of full native support on ARM at that time I went off and used node.js and the onoff package to write code in Javascript to manipulate the GPIO pins. That experimentation proved quite successful. But I wanted the same capabilities using Go if at all possible for one very important reason: Go compiles down to a single binary that doesn’t need a special runtime and supporting packages to be installed.

Recently I came across Embd, a Golang-based framework for manipulating all of a Raspberry Pi’s peripherals. It also claims to perform the same way on the Beagle Bone Black, another strong attractor for me as I have a BBB as well. I installed Go V1.6.2 (the latest release) using the standard Arch Linux ARMv7 packages. git was also installed on the RPi3, so I grabbed a copy of embd on Github (go get github.com/kidoman/embd). Once that was accomplished I started to write simple Go applications and tried to see how it operated. And I ran into more than a few problems getting it to work.

An example of a problem I ran into, and my solution to prove that at least a part of embd works, was the sample application fullblinker.go. It’s a test application that’s supposed to toggle the green LED, next to the red power LED, on the corner of the RPi board. As originally delivered it turned the LED on, but it was a solid light, not blinking. After rummaging around a bit in the source, I modified fullblinker to actually blink the LED on the RPI3, then copied it over to the RPI2 and tested it successfully as well. The photo above shows the on state of the LED during the blink test.

// +build ignore

// LED blinker, works OOTB on a RPi.

package main

import (
	"flag"
	"os"
	"os/signal"
	"time"

	"github.com/kidoman/embd"
	_ "github.com/kidoman/embd/host/rpi"
)

func main() {
	flag.Parse()

	if err := embd.InitLED(); err != nil {
		panic(err)
	}
	defer embd.CloseLED()

	led, err := embd.NewLED(0)
	if err != nil {
		panic(err)
	}
	defer func() {
		led.Off()
		led.Close()
	}()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt, os.Kill)
	defer signal.Stop(quit)

    var isOff   bool = true

	for {
		select {
		case <-time.After(250 * time.Millisecond):
            if isOff {
                isOff = false
                if err := led.On(); err != nil {
				    panic(err)
			    }
            } else {
                isOff = true
                if err := led.Off(); err != nil {
				    panic(err)
			    }
            }

		case <-quit:
			return
		}
	}
}

The changes I made to the code are highlighted above. I had to replace the Toggle() function with logic that maintained the LED on or off state, and explicitly call the On() and Off() functions (highlighted lines 43 to 53). Once flashing, this at least validated part of embd. One requirement for running fullblinker is the need to run with sudo (as root), which I’d managed to eliminate with the onoff nodejs GPIO application by using a special group with udev rules. Which appears to cause additional complications with embd, as none of the embd GPIO examples work, failing with a “write /sys/class/gpio/export: device or resource busy” error message. I’ve yet to dig into that problem, but I have several ideas on how to proceed to debug the issue.

embd is a start, along with node.js and onoff. I much prefer how the Javascript application was easy to set up, and I’m mulling over the idea of replicating the Javascript API in spirit to an equivalent GO framework, using what I can pick up from embd.

At this point you’re probably asking why bother? It’s so boring, so mundane. When Google talks about it’s AI TPU, or the net chatters about the rivalry between VR platforms, writing at this low a level on a $35 computer seems like such a waste of time. I can assure you it isn’t. Sooner or later you get back down to this level, and if it doesn’t work at this level then everything further up the stack goes to shit. One major driver to my working on this is my deep appreciation of the Go language, and my desire to use it here. Once I get it working to my satisfaction I’ll have tools that I can build upon, and perhaps something I can share back with the community. I’m just a regular engineer, not a disrupter, and as the Guardian once wrote, it’s the regular people who keep the world afloat.

adding go to yosemite

January 24, 2015

Screen Shot 2015-01-24 at 5.13.00 PMI’ve also added the Go programming language to my collection of tools on the Mac Mini. Installation of a basic Go environment, which is just the language and all its support tools, is dead simple. Just head over to download Go and follow the instructions there for installing Go.Screen Shot 2015-01-24 at 8.47.27 AMScreen Shot 2015-01-24 at 8.47.37 AMScreen Shot 2015-01-24 at 8.47.47 AMScreen Shot 2015-01-24 at 8.48.12 AMScreen Shot 2015-01-24 at 8.48.28 AMInstallation is as simple as the five steps shown above. Just know that when you click to install that you’ll have to type your login account password to fully install. Also note that the default location for go is under /usr/local (/usr/local/go). For Go to be fully visible at the command prompt you’ll need to add /usr/local/go/bin to $PATH. You’ll also want to define your $GOPATH environmental variable in your shell. That’s the root directory down which you’ll keep your sources as well as where other Go utilities will be stored.

For development tools I’m staying away from vi (Vim) as it’s an older version (7.3) that what you’ll find in the Linux distributions. I don’t feel the need to hassle with installing the later version. I tried IntelliJ’s IDEA with a Go plugin, but that didn’t work out too well. Right now I’m giving Sublime Text 2.0.2 a spin. Sublime Text is supposed to have a Go plugin to help with Go coding, but I don’t have it installed right now. Sublime Text is a paid application (US$70), and I’m about ready to spend the cash. Sometimes all I need is a good editor, and Sublime Text looks to be that good editor, at least on Yosemite.

golangOver the weekend, in between all the other tasks I do as husband, father, home owner and tax payer, I cobbled together a Go programming environment using Vim on my Ubuntu 14.10 notebook. Adding this capability required that I install Go, Go tools, and a series of Vim plugins pulled from Github.

Why Go?

I chose to go to Go (or golang) as a programming environment, specifically on the Raspberry Pi, because I’d finally had my fill of the node.js and the Io.js fork melodrama. I’d already grown quite tired of how Joyent was handling the stewardship of node.js and was loosing faith that node.js would stay up to date, such as running with Google’s latest V8 Javascript engine. In the end I decided I needed more of a mature language and supporting development tools written and managed by real adults. That’s when I decided to give a go with Go.

Go, in case you’re curious, is aimed more at systems level programming (which is where I tend to reside). Go is also a Google product, invented by two of the most iconic hackers the software world knows: Rob Pike and Ken Thompson. I know of Rob Pike primarily through his book “The Unix Programming Environment” he wrote along with Brian Kernighan. I’ve had that book since the late 1980s, along with “The C Programming Language.” That latter book was co-written by Ken Thompson along with Brian Kernighan (as well). And, of course, in his spare time, Ken Thompson was the principal inventor of Unix. And they’re now both working for Google Research.

Beyond Go’s stellar heritage, there are many reasons to use Go. Two that come immediately to my mind are Go’s ability to work closely with C/C++ code and Dave Cheney’s “Unofficial ARM Tarballs for Go” for various ARM-based tiny computers. One of the unofficial ARM targets is ARM6 and the Raspberry Pi. A second target is ARM7 and the Beagle Bone Black Rev C. The goal of this exercise is thus in two parts: get Go tooling up and running under Ubuntu, start developing Go under Ubuntu, run the products of said development under Raspbian on the Raspberry Pi using one of the unofficial tarballs, then move the entire development environment over to the Raspberry Pi itself if all of that works satisfactorily.

If Go is a poor fit on the RPi and BBB, then I intent to drop back to good ol’ C and C++. I’m done with node.js and Javascript.

Golang Setup under Ubuntu 14.10

Setting up Go is straightforward in Ubuntu (or another other major OS). The best way is to go follow the steps on the Golang site: download, follow the directions exactly, and then you’re done. That sets up the initial Go build/run environment, and you can start writing code from that point. For this exercise I downloaded and installed Go 1.4.1.

But I wanted more. I wanted an IDE, or the equivalent thereof, for Go code development. I wanted to use Vim specifically because I didn’t want to have to run a graphical desktop on the Raspberry Pi; I wanted to run on the console. In the end I chose to use Vim as my Go IDE and found reasonably current directions at myitcv:blog, “Programming with Go in Vim.”

The blog entry has everything you need to do broken down in easy-to-follow steps. Here are my comments on those steps.

  1. My version of Vim was the same, but built October 2014.
  2. I downloaded and installed Go version 1.4.1.
  3. I know how to add Vim plugins, and that’s the route I chose.
  4. For installing godef, make sure you have Mercurial installed. I didn’t and installation failed the first time.
  5. I installed all the plugins via git clone from Github. I’m using Pathogen to rule them all.

Screenshot from 2015-01-18 23:08:53Here’s Vim performing an IDE-like function, code completion in a tiny snippet of Go code. It was this feature I wanted more than any other. I can keep the general language in my head, but code completion helps to “remember” it all. I do need to tweak the colors a bit; gray on purple doesn’t quite make it (white or yellow would be better), but it’s usable regardless. Here’s my .vimrc with what I’ve enabled so far. It will change quickly.

execute pathogen#infect()
filetype plugin on
set omnifunc=syntaxcomplete#Complete

colo elflord
syntax on
set nu

set autoread
set ruler
set cmdheight=2

set expandtab
set smarttab
set shiftwidth=4
set tabstop=4

set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

let g:godef_split=3
let g:godef_same_file_in_same_window=1

I’ll keep updating this as I go along, with seperate posts, but for now it’s operational enough for me to learn how to write Go code. More to follow…