Contents

Go

History

Go was developed in 2007 by Google to improve programming productivity in an era of multicore, networked machines and large codebases. Ken Thompson (inventor of UNIX and C), Rob Pike and Robert Griesemer are the people behind this languange.

The idea was to simplify the software development process. Since C++ was outdated and complex, development teams were less productive and wasting more on bug fixing.

In this era, applications and processing move to cloud and concurrency becomes very big issue. Cloud computing by their nature is share and scale resource. Managing access to shared resource is an issue that impacts every application in cloud. Go was created to address the concurrency issue for scaled applications, microservices. Go has strong ecosystem supporting service development. The major cloud provider have go APIs for their services. There so many popular open source libraries provide for API tooling (swagger), transport (protocol buffer, gRPC), monitor (OpenCensus), Object-Relational-Mapping (gORM). Almost 75% projects in Cloud Native Computing Foundation are written in Go

Feature Go

  1. Concurrency

Go has gorutine that make concurrency work natively in these system software. It is a function that can execute programs independently without disturbing the other function with a different program and method. Channels work as a medium of communication for Goroutines, making it work efficiently by communicating with other Goroutine threads. This goroutine is handled by go scheduler.

  1. Static Typing

Go is a statically typed programming language and works with a mechanism that makes it possible to compile code accurately while taking care of type conversions and compatibility level. This gives developers freedom from challenges associated with dynamically typed languages.

  1. Garbage Collection

The programming language also offers exceptional power of garbage collection. Meaning, developers need not worry about freeing up pointers or the situation associated with dangling pointer.

  1. The Go Standard Library

Go has standard library, which makes programmers lives much simpler. For one, Go offers world-class support for networking (specifically HTTP/2) and file management. It also offers native JSON encoding and decoding. As a result, setting up a server to handle HTTP requests and return responses (JSON or other) is extremely straightforward, explaining Go’s popularity for development of REST-based HTTP web services.

  1. Error handling

Errors in Go are handled rather differently from other languages. In short, Go handles errors by returning a value of type error as the last return value for a function.

When a function executes as expected, nil is returned for the error parameter, otherwise the error value is returned. The calling function then checks the error return value, and handles the error, or throws an error of its own.

1
2
3
4
5
6
7
8
// the function returns an int and an error
func calculateRemainder(numerator int, denominator int) (int, error) {
   // Error returned
   if denominator == 0 {
      return nil, errors.New("denominator is 0")
   }   // No error returned
   return numerator / denominator, nil
}
  1. Go has implicit interfaces

Program to an interface, not a implementation, an API should only publish the contract of its expected behavior (its method signatures), but not details about how that behavior is implemented.

Go has support for interfaces. In fact, it turns out interfaces are the only abstract type in Go.

Unlike other languages, however, interfaces in Go are not implemented explicitly, but rather implicitly. A concrete type does not declare that it implements an interface. Rather, if the method set for that concrete type contains all the method sets of the underlying interface, Go deems that the object implements the interface.

This implicit interface implementation (formally termed structural typing), allows Go to enforce both type-safety and decoupling, keeping much of the flexibility exhibited in dynamic languages.

Explicit interfaces, by contrast, bind the client and implementation together, making replacing a dependency in Java, for example, far more difficult than in Go.