18
Introduction to Golang
Programming
Language
Go
Parallel Programming
Programming Languages

I recently started Server side development with Golang(go). The first thing that comes to mind is

What is Go?

Go is a new, general-purpose programming language.

  • Compiled
  • Statically typed
  • Concurrent
  • Simple
  • Productive

Why Go?

  • Statically typed languages are efficient, but typically bureaucratic and overly complex.
  • Dynamic languages can be easy to use, but are error-prone, inefficient, and break down at scale.
  • Concurrent programming is hard (threads, locks, headache).

The creation myth of Go is something like this: Rob Pike, Ken Thompson, and Robert Griesemer were waiting for a particularly long C++ build to take place when they decided to theorize a new language. What started on a whiteboard in 2007, shortly thereafter turned into a specification, which then was open sourced in 2009 with two compiler implementations.

Hello,World

package main

import "fmt"

func main() {

    fmt.Printf("Hello, World")

}

Go’s purpose is therefore not to do research into programming language design; it is to improve the working environment for its designers and their coworkers. Go is more about software engineering than programming language research. Or to rephrase, it is about language design in the service of software engineering. - Rob Pike

The best place to get started with Go is by taking the go tour on https://tour.golang.org/welcome/1

Some of the Important points :

1.Go is an object oriented programming language.

It may not have inheritance but from an object oriented standpoint, Go does provides the ability to add behavior to your types via methods, allows you to implement polymorphic behavior via interfaces and gives you a way to extend the state and behavior of any existing type via type embedding. Go also provides a form of encapsulation that allows your types, including their fields and methods, to be visible or invisible. Everything you need to write object oriented programs is available in Go.

2.Go routines

A goroutine is a function that is capable of running concurrently with other functions. To create a goroutine we use the keyword go followed by a function invocation:

package main

import "fmt"

func f(n int) {

  for i := 0; i < 10; i++ {

    fmt.Println(n, ":", i)

  }

}

func main() {

  go f(0)

  var input string

  fmt.Scanln(&input)

}

This program consists of two goroutines. The first goroutine is implicit and is the main function itself. The second goroutine is created when we call go f(0). Normally when we invoke a function our program will execute all the statements in a function and then return to the next line following the invocation. With a goroutine we return immediately to the next line and don't wait for the function to complete.

There are many approaches to concurrency but they can be notoriously difficult to reason about. If you have had to debug a deadlock with mutexes, you don’t need me to elaborate on this. Go’s approach results in code that is easily understood as it can be reasoned about like a sequential program. That’s a huge plus.

3.Complexity

Finally, I think the Go designers wanted to keep the language simple (simple in the usual definition of simple).

  • How many loop keywords are there? 1 for
  • Why Garbage Collection? Because it’s easier while working with concurrency.
  • Why eliminate inheritance? Because it’s inflexible to change and composition is almost always better.
  • How many keywords? 25. vs 50 in Java and 48 in C++ Expressiveness of Go
  • Why CSP model for concurrency? Because mutexes and locking are a hard paradigm to get right.

4.Server Side

Go (Golang.org) language provides standard HTTP protocol support in its standard library, which makes it easy for developers to build and get a web server running very quickly.

"Hello,world" example in an HTTP web server version.

package main

import (
    "io"
    "net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "Hello world!")
}

func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe(":8000", nil)
}

Go provides a way to write systems and servers as concurrent, garbage-collected processes (goroutines) with support from the language and run-time.

If you’ve read this far, then you are most certainly a delightful person. I’ll write quite a bit more on Go, hopefully this was a helpful first step.

Author

Notifications

?