Go Language on OpenWRT – Carambola2

I’m playing with interesting go language which is built for system programming however, golang has been using to solve variety problems. For me, the most important aspect of the language is it is compiled and cross platform . Its simple, easy to learn and solves resource problems efficiently.

For test purposes, I wrote a simple hello world application, which dumps  struct as hex string on Carambola2 (MIP32 arch.). To be able compile go source code on OpenWRT gccgo is required. There are OpenWRT repos adds go lang support to gcc for MIPS32 architecture.

If target environment doesn’t contain go library (libgo.so) , source code should be compiled with “-static-libgo” flag. In my case;

mips-openwrt-linux-gccgo -g -static-libgo hello.go -o hello2

Output ;

root@Carambola2:~# ls -lah
drwxr-xr-x 1 root root 0 Aug 18 18:09 .
drwxr-xr-x 1 root root 0 Aug 18 17:58 ..
-rwxr-xr-x 1 root root 16.5K Aug 18 2016 hello
-rwxr-xr-x 1 root root 2.8M Aug 18 18:09 hello2
root@Carambola2:~# ./hello2
00000000 01 02 |..|

root@Carambola2:~#

Script;

package main

import (
       "fmt"
       "bytes"
       "encoding/binary"
       "encoding/hex"
)

type Data struct {
       X uint8
       y uint8
}

func main() {
       var buf bytes.Buffer
       dt := Data{1, 2}

       binary.Write(&buf, binary.BigEndian, dt)
       fmt.Println(hex.Dump(buf.Bytes()))
}

 

Also see this blog post further information.