Install and Configure Go/Golang on Raspberry Pi

Go/Golang is one of the hot programming languages while I’m typing this today.

Go is a programming language created at Google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson. Go is a statically typed compiled, procedural language similar to C, with memory safety, garbage collection, structural typing, concurrency and other great features are bundled to make it better compared to other languages in the marketplace.

Docker, Kubernetes, Graphana, Hugo are some of the best apps written in Go. It has a robust set of libraries and app performance is better compared to other languages.

Today I’m starting my journey to learn Go/Golang and Google will be my mentor to install and setup Golang on my Raspbian OS/Raspberry Pi 3. To get the latest version use below steps instead of native package management tool such as apt.

Installation Steps:

  • Download the current stable version of Go available on the google’s official website. At the time of writing this tutorial, 1.12.4 is the stable version. Check the latest version here
cd ~ && curl -O https://dl.google.com/go/go1.12.4.linux-armv6l.tar.gz

Above command would change directory to your ‘Home’ directory and download Go compressed tar file using ‘Curl’

  • Extract compressed tar file and place it inside /usr/local directory. Please note root level access or sudo access required to perform this step.
sudo tar -C /usr/local -xzvf go1.12.4.linux-armv6l.tar.gz
  • Set Path variables to avoid typing complete path and in order to access binaries or libraries of Go by the Raspbian OS. Open ~/.profile, a hidden file located in your ‘Home’ directory. Use nano or vi or subl to edit the file (subl ~/.profile). Add below lines at the end of the file.
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
  • To take effect of the above changes made to the file ‘profile’, run the below command. The source command can be used to load any functions file into the current shell, script or a command prompt.
source ~/.profile
  • Create directory called ‘go’ in the ‘Home’ directory. All my codes are placed in the folder. Change the directory name as per choice but do change the GOPATH accordingly as mentioned above.
 mkdir $HOME/go 
  • Validate Go is working as expected or not by running below command
vb@pi:~ $  go version
go version go1.12.4 linux/arm
vb@pi:~ $

My first code in Go

Create a directory first_code in ‘go’ and write following content in the file and save it as first_code.go

mkdir -p $HOME/go/src/first_code 
{-p is used to create directory and its sub-directories at once}
package main

import "fmt"

func main() {
    fmt.Printf("My first code in Go Language!!!\n")
}

Now build and run the code. Change directory
to the first_code cd ~/go/src/first_code and run below commands

vb@pi:~/go/src/first_code $ go build
vb@pi:~/go/src/first_code $

vb@pi:~/go/src/first_code $ ./first_code
My first code in Go Language!!!
vb@pi:~/go/src/first_code $

Note:

Above steps are applicable to any Linux distributions just by changing the first step of downloading the compressed tar file. Change the architecture from arm6l to amd64 or as applicable to your hardware.

I am able to successfully setup GO on my Pi. Hope you’d also do the same and happy learning Go. If you have any issues or questions please mention them in the below comment section.

Advertisements

Python’s sh Library

Wish you all Happy, Prosperous & Pythonic New Year 2019!!

I thank everyone for stopping by my blog for 15K times in 2018 which is the highest so far! 

“Looking back at my life’s voyage, I can only say that it has been a good trip.” ~ Ginger Rogers

Similar to the above quote, I’d started my scripting voyage by writing shell, batch and Perl scripts in good old days. I miss some of the awesome, builtin & yet simple to use features available in shell. I felt using those features or builtins would make my life easier while writing scripts in Python…

Besides that, one main reason to look back at the past was while imparting the Python Workshop at my office. One of the participants raised concerns over Python’s builtin modules like OS and Subprocess. They mentioned we can accomplish with very simple steps in ‘Shell’ instead of using OS or Subprocess module. I had to accept the fact that sometimes Python is not so easy as we think!

After googling for a while I finally found this… Say hola to ‘sh‘ Module!

The ‘sh’ library provides simple and intuitive alternative to OS/System/Subprocess modules.

‘sh’ module simplifies Python’s ability to interact with native OS by calling shell commands. This would greatly helps in automation of routine tasks and running sequence of commands, parsing output as per requirement.

Python is a powerful language powered by those great developers who are continuously contributing to enable new features every day in day out. I would like to thank Andrew Moffat for the ‘sh’ library. This library is one of my favourites.

To install this library using PIP, run below command

pip3 install sh 

Please find sample scripts output. 

>>> import sh

>>> sh.uname()

Darwin

>>> sh.uptime()

 7:13  up 11 days, 23:29, 4 users, load averages: 1.51 1.65 1.88

>>> 

>>> sh.echo(‘Hello, This is shell\’s echo running from Python’)

Hello, This is shell's echo running from Python

>>> sh.who()

vinay    console  Dec 21 07:45 

vinay    ttys000  Dec 21 07:46 

vinay    ttys001  Dec 21 07:46 

vinay    ttys003  Jan  2 07:02 

>>>sh.df()

Filesystem    512-blocks     Used Available Capacity iused               ifree %iused  Mounted on

/dev/disk1s1   236568496 96124200 131754736    43% 1042911 9223372036853732896    0%   /

devfs                376      376         0   100%     653                   0  100%   /dev

/dev/disk1s4   236568496  6291496 131754736     5%       3 9223372036854775804    0%   /private/var/vm

map -hosts             0        0         0   100%       0                   0  100%   /net

map auto_home          0        0         0   100%       0                   0  100%   /home

>>> sh.ifconfig()

lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384

options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>

inet 127.0.0.1 netmask 0xff000000 

inet6 ::1 prefixlen 128 

inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 

nd6 options=201<PERFORMNUD,DAD>

gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280

stf0: flags=0<> mtu 1280

XHC20: flags=0<> mtu 0

en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

ether b8:e8:56:36:0b:58 

inet6 fe80::149c:1b88:578c:73f8%en0 prefixlen 64 secured scopeid 0x5 

inet 192.168.1.9 netmask 0xffffff00 broadcast 192.168.1.255

nd6 options=201<PERFORMNUD,DAD>

media: autoselect

status: active

p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304

ether 0a:e8:56:36:0b:58 

media: autoselect

status: inactive

awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484

ether 8a:0c:61:9e:64:9b 

inet6 fe80::880c:61ff:fe9e:649b%awdl0 prefixlen 64 scopeid 0x7 

nd6 options=201<PERFORMNUD,DAD>

media: autoselect

status: active

en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500

options=60<TSO4,TSO6>

ether 72:00:00:20:b1:90 

media: autoselect <full-duplex>

status: inactive

en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500

options=60<TSO4,TSO6>

ether 72:00:00:20:b1:91 

media: autoselect <full-duplex>

status: inactive

bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500

options=63<RXCSUM,TXCSUM,TSO4,TSO6>

ether 72:00:00:20:b1:90 

Configuration:

id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0

maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200

root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0

ipfilter disabled flags 0x2

member: en1 flags=3<LEARNING,DISCOVER>

        ifmaxaddr 0 port 8 priority 0 path cost 0

member: en2 flags=3<LEARNING,DISCOVER>

        ifmaxaddr 0 port 9 priority 0 path cost 0

nd6 options=201<PERFORMNUD,DAD>

media: <unknown type>

status: inactive

utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000

inet6 fe80::bd0:9dea:48de:5645%utun0 prefixlen 64 scopeid 0xb 

nd6 options=201<PERFORMNUD,DAD>

>>> 

Thanks for stopping by, please share your comments and ideas to improve this blog. Keep watching for more Python libraries used for automation.

Manage services from CLI on Linux

In Window$ OS, we can check running services and its status by going to Services console. (Start > Run > services.exe)

Similarly, to access running services info and status under Linux via BASH / Shell, we need to install ‘sysv-rc-conf’

aptitude install sysv-rc-conf

Below screenshot show the output of sysv-rc-conf command displaying status of service for all running levels (1-6) and startup (S)

2016-11-21 11_06_38-Debian1 [Running] - Oracle VM VirtualBox.png

As you can see in the screenshot to start a service use ‘+’ and to stop a service use ‘-‘. To enable or disable service to run at startup goto the respective service’s ‘S’ column and then use ‘Space Bar’.

GNU/Parallel – Run multiple commands simultaneously

GNU/Parallel is an open source tiny executable yet so powerful to run / start multiple commands at the same time. This is a handy tool when we are trying to run multiple scripts to maintain consistency of timestamp across the board.

gnu-parallel

Below is a simple file named as REST to execute Python RESTAPI scripts simultaneously

date > start.$today.array;python sym_perf_stats_v4.py &>> /vin/array.$today.log; date > end.$today.array
date > start.$today.fe;python fe_perf_stats_v3.py &>> /vin/fe.$today.log; date > end.$today.fe
date > start.$today.be; python be_perf_stats_v3.py &>> /vin/be.$today.log; date > end.$today.be
date > start.$today.diskgrp ; python dg_perf_stats_v3.py &>> /vin/diskgrp.$today.log; date > end.$today.diskgrp
date > start.$today.tp ; python tp_perf_stats_v3.py &>> /vin/tp.$today.log; date > end.$today.tp

to run the above scripts in parallel we need to execute the below command

#parallel -j 5 <REST

-j switch used to run ‘n’ number of commands in parallel from the same console

***UPDATE *** As per suggestion from oletange. Above file ‘REST’ can be better written as below

parallel ‘date > start.$today.{};python {}_perf_stats_v3.py &>> /vin/{}.$today.log; date > end.$today.{}’ ::: sym fe be dg tp

To install GNU/Parallel on Debian / Debian based OS

#apt-get install parallel

 

Wire free connect to Android Phone via SSH

I left my USB Type C cable cum charger in office yesterday and couldn’t transfer some files. I am aware of Shareit app but it doesn’t work in Linux. Both my laptop and phone are connected to wireless router.

After googling for while found a native and simplest way to connect to my android phone via SSH.

Installed SSHDroid on my phone and started the SSH service. From my Debian GNU Linux console started SSH session and connected to the IP of my phone and using SCP copied all those required files from phone to PC. BTW copied new songs from PC to phone as well 🙂

Simple snapshot tool for Linux

I was looking for a simple yet powerful backup / snapshot tool which would help me in taking regular backups of my Raspberry Pi. Pi is my personal AIO server running website https://vinaybabu.in, LAMP stack, Mail server, NAS services etc…

I’ve chosen rsnapshot which is based on rsync. This tool suits perfectly for my backup / snapshot requirement and works like a charm.

Download a copy and give it a try. Happy learning and Backup-ing!!

How to quickly setup Squid Proxy?

Squid proxy is one of the best open source proxy software’s and most widely implemented solutions. I was trying to quickly set this up with less efforts but with no compromise w.r.t implementing robust ACLs (Access Control Lists).

Instead of reading various Howto’s in the net, I started searching for ready made appliance which could make things easy for me 🙂

logo

After reviewing various options, I have had decided to go with Artica Proxy. Its pretty simple and its intuitive interface makes it simple to swiftly setup Squid Proxy within few minutes.

It offers Transparent Proxy, Load Balancer, 1.6 Millions blacklisted sites (updated on daily basis), user defined ACLs etc… For a full featured characteristics and benefits please visit the URI here.

I hope you’ll find this intelligent software useful and happy proxying!!