Pages

Showing posts with label Google. Show all posts
Showing posts with label Google. Show all posts

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.

Sunday, 12 April 2015

Coding in Dart

The Dart programming language was introduced around October 2011. Last year in July 2014 I downloaded the stable Dart SDK version 1.8.3 to experiment with this relatively new language. From the onset, let me list the key objective of Dart as espoused by Google:
  • Dart is a structured language with optional typed capability.
  • Dart is targeted at both client and server side development.
  • Dart is an alternative to JavaScript coding.
I have successfully developed a couple web applications where the browser side is Dart and the server side is PHP. I have tested several features such as Json, MySQL and an Http server. The image below (I could not figure out how to insert formatted text) is a screenshot of the entire code for sending an email using my Google credentials. It works flawlessly, though I still have to test send an email with an attachment or two:


Dart Editor



You may have recognised the similarity of the Dart editor to Eclipse, particularly if you have used Google's SDK editor for native Android development.

In terms of serve side development, Dart's inherent asynchronous feature can lead to some unexpected results. A good grasp of the "Futures" feature will go a long way in writing code that meets your expectation in the sequence of event that should result in predictable output.

Google for dartlang and explore the suitability of Dart in your new projects - even if it for experimental purposes only.