Pages

Wednesday 30 November 2016

Coding in Go

In one of my previous posts I may have commented about Google's programming language called Go, also referred to as Golang. Its origins, evolution and historical background is very well documented in a number of website, so I will not spend to much effort on it in this post.

I became aware of Golang while I was learning to code in Google's other programming language cold Dart. Having primarily been a Pascal and PHP programmer, Golang's conspicuous C like syntax initially was a mild concern. Fortunately this concern was short-lived and generously offset by Golang's extensive list of features such as (to mention a few):
  • Cross-Platform compatibility.
  • Native support for concurrency paradigms.
  • Develop executables.
My taste for software development is inclined towards certain type of applications, each include some level of a relational database integration:
  • Web portals (60%)
  • Native Windows GUI (30%)
  • System administration scripts (10%)
Golang seems to fit very well for the first and last application types.  I have already developed a couple sample application, mainly for tutorial purposes and I must confess, the language is fun. Below is sample code that pings several IP addresses and displays the ping results in the command line window:

package main

import "os/exec"
import "strings"
import "time"
//import "log"
//import "errors"
import  "fmt"


func insertSection_TicketSummary() {
 // var mystr string
 //
 // mystr = ""
}

func pingHost(aHost string, aStatus chan bool) {
 var mystr string

 //fmt.Printf(" -->Pinging host with IP address: %s\n", aHost)

 output1, err := exec.Command("ping", "-n", "1", aHost).Output()

 if err != nil {
  //fmt.Printf("%s", err)
  }

 //fmt.Printf("%s\n", output1)
 mystr = string(output1)

 //fmt.Printf("%s\n", mystr)
 //i int

 i := strings.Index(mystr, "(0% loss)")

 if i > 0 {
  //fmt.Printf("%s\n", "Host is online")
  fmt.Println("-> Host " + aHost +" is online")
 } else {
  fmt.Println("-> Host " + aHost +" is offline")
 }

 aStatus <- true

 //time.Sleep(time.Second * 2)
}

func main() {
 t := time.Now().Local()

// fmt.Println(t)
 fmt.Println(t.Format("2006/01/02 15:04:05"))    // 31/12/2015 08:34:12

 //Create a new channel to follow excution status of goroutines:
 done := make (chan bool)

 // send two pings and send the ouput to STDOUT
 //Linux mode:
// output1, err := exec.Command("ping", "-c", "2", "8.8.8.8").Output()

 //Declare an array of hosts and associated ip addresses:
 hostsRouters := []string {"192.168.20.1",
       "192.168.21.1",
      "192.168.22.1"}

 hostsSwitches := []string {"192.168.20.3",
        "192.168.21.3",
                      "192.168.22.3"} 
 
 
        hostsPrinters := []string {"192.168.20.99",
              "192.168.21.99",
              "192.168.22.99"}
            "10.18.65.10"}

  hostsInternet := []string {"8.8.8.8"}

 //Quantify the number of nodes to be pinged:
 var nodeQty int
 nodeQty = len(hostsRouters)    +
    len(hostsSwitches)   +
    len(hostsPrinters)   +
    len(hostsInternet)

 //Windows mode:
 fmt.Println("Pinging MPLS WAN provincial routers:")
 for i := 0; i < len(hostsRouters); i++ {

  go pingHost(hostsRouters[i], done)
 }

 //var input string

 fmt.Println("Pinging MPLS WAN provincial switches:")
 for ps := 0; ps < len(hostsSwitches); ps++ {

  go pingHost(hostsSwitches[ps], done)
 }

 fmt.Println("Pinging MPLS WAN provincial MFP printers:")
 for pp := 0; pp < len(hostsPrinters); pp++ {

  go pingHost(hostsPrinters[pp], done)
 }

 fmt.Println("Pinging hosts internet:")
 for pi := 0; pi < len(hostsInternet); pi++ {

  go pingHost(hostsInternet[pi], done)
 }

 for fr := 0; fr < nodeQty; fr++ {
  <- done
 }

 //<- done

}
This simple code highlight  a few features of Golang (in no particular order):
  • date assignments.
  • array declaration.
  • for loop.
  • function declaration.
  • channel assignments.
  • go routines.
If you have never done any coding in Golang, I suggest you start right now and experiment for yourself the potential benefits of Google Go.