Monday, December 9, 2024
Tech/Web

The Go Programming Language

The Go Programming Language is an open source, compiled, concurrent, garbage-collected, statically typed language developed at Google. Google created it to solve its own problems in relation to multicore processors, networked systems and large codebases. While languages such as C and C++ are fast, they are hard to master and development takes longer. While Python is easier to work with, it compromises on runtime efficiency. Go was designed to be efficient, scalable and productive.

This programming language designed by Google engineers Robert Griesemer, Rob Pike, and Ken Thompson.

Try it now https://play.golang.org/

Why was Go invented?

Go was invented (in 2007) at a time when multicore CPU architectures were common, largely because Moore’s Law was failing to deliver. With multiple cores, code can run in parallel on all available cores; but there was no programming language that simplified the development of multithreaded applications. The responsibility of managing different threads safely and efficiently fell on developers. Thread-locking, race conditions and deadlocks are usual challenges. Go was designed with concurrent programming in mind.

Adoption of Go language

GO adopted by Docker, Kubernetes, Weave, Shipyard, etcd, Fleet, Deis, Flynn. Since Go is suited to distributed server apps, all of these apps are cloud related. Lime is an example of a desktop app for text editing. InfluxDB, a distributed time series database, is written in Go. Gogs (Go Git Service) helps you to run a Git service, like GitHub. Go is a suitable fit for Docker due to static compilation and ability to build for different architecture with less effort. In 2016, Go was rated the most popular language in the TIOBE index. Multiple developer surveys in 2018 show interest and adoption of Go.

Features of Go language

  • Concurrency
  • Scalability
  • Fast performance
  • Garbage collection and cross platform
  • Compiled language
  • Use as object-oriented language

What are some criticisms of Go Programming Language?

  • Go doesn’t support generics
  • Packages are not versioned in a centralized place.
  • We can’t define immutable data types
  • Compile-time checks are lacking.
  • It’s slow compared to C, C++ and Rust.
  • Interfaces are implicit.

Why IoT Should Pay Attention To Go

In IoT framework requires a good and dependable programming language. If you look at many of the tools today, you really only have  C/C++ or a NodeJS integration, which lacks modular support cause they are generally designed to work with only one particular device. Let me introduce you to using Go as part of your IoT solution.

As a relatively new language that was inspired by C roots without all of its dangers, built in concurrency sweetens the deal further by allowing you to define goroutines that plug straight into your embedded devices. The concurrency model here is much easier and safer to implement. Remember the last time you had to deal with pthreads along with the mutexes, condition variables and semaphores?

It’s probably Go’s intention to not be part of the embedded toolchain. It was not designed to replace embedded C, but more towards handling the communication layer to and from these devices. The garbage collection in Go makes it less ideal for it to implemented at the embedded device level, but great for people seeking greater control of resources without paying too much overhead for such features.

Sample code of Go Programming Language:

// This program solves the (English) peg
// solitaire board game.
// http://en.wikipedia.org/wiki/Peg_solitaire

package main

import "fmt"

const N = 11 + 1 // length of a row (+1 for \n)

// The board must be surrounded by 2 illegal
// fields in each direction so that move()
// doesn't need to check the board boundaries.
// Periods represent illegal fields,
// ● are pegs, and ○ are holes.

var board = []rune(
    `...........
...........
....●●●....
....●●●....
..●●●●●●●..
..●●●○●●●..
..●●●●●●●..
....●●●....
....●●●....
...........
...........
`)

// center is the position of the center hole if
// there is a single one; otherwise it is -1.
var center int

func init() {
    n := 0
    for pos, field := range board {
        if field == '○' {
            center = pos
            n++
        }
    }
    if n != 1 {
        center = -1 // no single hole
    }
}

var moves int // number of times move is called

// move tests if there is a peg at position pos that
// can jump over another peg in direction dir. If the
// move is valid, it is executed and move returns true.
// Otherwise, move returns false.
func move(pos, dir int) bool {
    moves++
    if board[pos] == '●' && board[pos+dir] == '●' && board[pos+2*dir] == '○' {
        board[pos] = '○'
        board[pos+dir] = '○'
        board[pos+2*dir] = '●'
        return true
    }
    return false
}

// unmove reverts a previously executed valid move.
func unmove(pos, dir int) {
    board[pos] = '●'
    board[pos+dir] = '●'
    board[pos+2*dir] = '○'
}

// solve tries to find a sequence of moves such that
// there is only one peg left at the end; if center is
// >= 0, that last peg must be in the center position.
// If a solution is found, solve prints the board after
// each move in a backward fashion (i.e., the last
// board position is printed first, all the way back to
// the starting board position).
func solve() bool {
    var last, n int
    for pos, field := range board {
        // try each board position
        if field == '●' {
            // found a peg
            for _, dir := range [...]int{-1, -N, +1, +N} {
                // try each direction
                if move(pos, dir) {
                    // a valid move was found and executed,
                    // see if this new board has a solution
                    if solve() {
                        unmove(pos, dir)
                        fmt.Println(string(board))
                        return true
                    }
                    unmove(pos, dir)
                }
            }
            last = pos
            n++
        }
    }
    // tried each possible move
    if n == 1 && (center < 0 || last == center) {
        // there's only one peg left
        fmt.Println(string(board))
        return true
    }
    // no solution found for this board
    return false
}

func main() {
    if !solve() {
        fmt.Println("no solution found")
    }
    fmt.Println(moves, "moves tried")
}

You may like also:  Gobot – A Framework for robots, drones, and IoT


 

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

One thought on “The Go Programming Language

Leave a Reply

Your email address will not be published. Required fields are marked *