Mphasis Digital Use Go For System Programming
Mphasis Digital Use Go For System Programming
Mphasis Digital Use Go For System Programming
Go is designed with the philosophy of “Less is exponentially more”. In a blog post dated June 2012, Rob Pike, one of the
• Go language was originated as an outcome of an experiment by Robert Griesemer, Rob Pike and Ken Thompson at
• Officially announced in November 2009, it is used in some of Google's production systems as well as by other firms
that have received success in recent times, including Martini, Gin, Negroni, Revel, Beego, Gorilla and Net/HTTP. Go is
believe that in future Go would come out as a strong alternate of C and C++ in the area of system programming, web
0000001110000100100100100010000100001000010000100001100101001010100
Go is also well suited for web development. There are multiple web development frameworks and middle tiers for Go
Go is not an object-oriented language. While Go support types, methods and interfaces, it does not support classes,
also supported by multiple PaaS platforms including Google Cloud Platform (App Engine), Cloud Foundry (including
designers of the language, reported how a language with less but well-designed features is far better than a language.
00000001110000100100100100010000100001000010000100001100101001010100
When you think about system programming,
think Go - developed at Google for advanced
statically typed language following the footsteps of system programming languages such as C and C++. But Go is
00000001110000100100100100010000100001000010000100001100101001010100 programming and for building large scale distributed systems. It is natively compiled (does not need a VM to run),
00000001110000100100100100010000100001000010000100001100101001010100
Pivotal Web Service) and Heroku. AWS Elastic Beanstalk also supports deploying Go applications. We strongly
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
Mphasis
C and C++ are widely accepted and used as de-facto standard for systemprogramming language.In spite of the
00000001110000100100100100010000100001000010000100001100101001010100
Go or Golang is an open source programming language developed at Google, specially targeted at system
00000001110000100100100100010000100001000010000100001100101001010100
• Version 1.0 was released in March 2012 and between 2012 and 2015, multiple versions were released
000000011100001001001001000100001000100
technology demands
000000011100001001001001000100001000100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
000000011100001001001001000100001000100
000000011100001001001001000100001000100
000000011100001001001001000100001000100
• Version 1.5 was released in August 2015, which is the current stable version
000000011100001001001001000100001000100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
development, large scale distributed systems and cloud workloads.
000000011100001001001001000100001000100
differentiated approach than C and C++. We believe this is
In recent past (since 2000) we have seen three moderately
C++, but also the flexibility of other modern languages such
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
successful system programming languages being born
00000001110000100100100100010000100001000010000100001100101001010100
– D (in 2001), Go (in 2009) and Rust (in 2012). Among
them, while D is a close follower of C and C++, if not
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
0000001110000100100100100010000100001000010000100001100101001010100
Overview of Go
00000001110000100100100100010000100001000010000100001100101001010100
History of Go
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
Abstract
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
00000001110000100100100100010000100001000010000100001100101001010100
000000011100001001001001000100001000100
000000011100001001001001000100001000100
New Programming Languages
Programming Backed by / First public
Success Paradigm Suitable for
Language Developed at release
System programming,
Procedural - Designed from ground up with
Go (also called November large scale distributed
Google High concurrency in mind. Supports Actor model
golang) 2009 systems, cloud workloads,
based concurrency.
web back ends
System programming,
December Procedural,
D Facebook Medium large scale distributed
2001 Object-oriented
systems, web back ends
Procedural, Object-oriented with functional
Rust Mozilla 2010 Medium System programming
programming constructs.
Object-oriented with functional and procedural Statistical computing, Big
R Bell Laboratories Late 90s High
constructs. Data analytics
Object oriented with influences functional
Scala Open Source 2004 High programming Programming. Supports actor General purpose
model based concurrency.
Apache Object oriented programming language with General purpose,
Groovy 2004 Medium
Foundation dynamic typing – designed for scripting in mind scripting focused
Clojure Open Source 2007 Low Functional – Is a dialect of Lisp General purpose
Object-oriented with scripting and functional
programming constructs. Designed from group
Dart Google 2011 Low Web development
up for web development. Supports Actor model
based concurrency.
Scripting language with Object oriented
TypeScript Microsoft 2012 Medium constructs. Designed from group up for web Web development
development
Functional programming language that supports
F# Microsoft 2005 Medium General purpose
object oriented concepts
Ceylon Red Hat 2011 Low Object oriented General purpose
Object oriented with influences from functional
Swift Apple 2014 High General purpose
programming
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 3
Go Usage
• Many Google web properties and systems including YouTube, Kubernetes containers and download server
dl.google.com
• Docker, a set of tools for deploying Linux containers
• Dropbox, migrated some of their critical components from Python to Go
• SoundCloud, for many of their systems
• Cloud Foundry, a platform as a service (PaaS)
• Couchbase, Query and Indexing services within the Couchbase Server
• MongoDB, tools for administering MongoDB instances
• ThoughtWorks, some tools and applications around continuous delivery and instant messages
• SendGrid, a transactional email delivery and management service
• The BBC, in some games and internal projects
• Novartis, for an internal inventory system
For a complete list of apps and systems that use Go refer-
https://en.wikipedia.org/wiki/Go_(programming_language)#Notable_users and https://github.com/golang/go/wiki/GoUsers
Structs and
Methods
Pointers
Actor Model
Based Interface
Concurrency
First class
Rich
Standard functions
Library
fmt.Println(add(20,30))
fmt.Println(sub(20,30))
}
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 4
Functions can be passed to other functions as parameters. In the below example higherOrderFunc is a higher order
function that accepts another function (f func(x int, y int) int) as parameter -
func main(){
add := func(x int, y int) int {return x+y}
var sub = func(x int, y int) int {return x-y}
fmt.Println(add(20,30))
fmt.Println(sub(20,30))
func higherOrderFunc(f func(x int, y int) int, num1 int, num2 int) int{
return f(num1 * num1, num2 * num2)
}
Now the function definition for higherOrderFunc is a bit complicated and could get even more complicated if it
accepts multiple functions as input parameter. This could be simplified by defining a user defined function type.
func main(){
add := func(x int, y int) int {return x+y}
var sub = func(x int, y int) int {return x-y}
fmt.Println(add(20,30))
fmt.Println(sub(20,30))
type HigherFunc func(x int, y int) int // user defined function type
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 5
Structs and Methods
Structs
Go does not support Class, but supports Struct. Structs in Go are typed collections of named fields similar to other
programming languages like C++ and Java. Structs can have methods apart from fields. Struct allows grouping of
fields (data) and methods (behavior).
type Employee struct {
name string // field name of type string
age int // field age of type int
salary float32 // field salary of type float32
designation string // field designation of type string
}
Structs could be initialized either using new function (Option 1) or using JavaScript like object literal notation (Option 2)
// Option 1 – using new function
emp := new(Employee)
emp.name = "Ken Thompson"
emp.age = 50
emp.salary = 12345.678
emp.designation = "Distinguished Engineer"
Another alternate is to specify field values as parameters in the same order fields are declared
// Option 3 – parameters should be in the same order fields are declared
emp := Employee{"Ken Thompson", 50, 12345.678, "Distinguished Engineer"}
Note:
; are optional in Go similar to JavaScript.
Local variables could be assigned without specifying their types using := instead of =
func main(){
address := Address{"MG Road", "Blore", "IN"}
skills := [4]string {"C","Go","Rwust"}
emp := Employee{"Aniruddha", 40, 123.456, skills, address}
fmt.Println(emp) // {Aniruddha 40 123.456 [C Go Rust] {MG Road Blore IN}}
fmt.Println(emp.Skills) // [C Go Rust]
fmt.Println(emp.HomeAddress.StreetAddress) // M G Road
}
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 6
Methods
Apart from member fields, Go structs support defining methods (member functions). Methods of the struct are actually
defined outside the struct declaration, which is unique in Go.
type Employee struct {
name string
age int
designation string
}
func main(){
emp := Employee{name:"Bill G", age:55, designation:"President"}
emp.Display() // prints Name: Bill G , Designation: President
}
func main(){
emp := Employee{name:"Bill G", age:55, designation:"President"}
emp.Display("Hello")
}
Interfaces
Go support interfaces, just like C++, Java and C# do.
Interfaces are named collections of method signatures.
They could be treated as blueprint as they do not provide
implementation. Types that implement an interface is Go simplifies the process and reduces a lot of
responsible for providing the actual implementation.
Go is unique in the fact that types do not have to explicitly effort from programming perspective.
use “implements” or other similar declarations to specify
that they are implementing an interface. To implement an
interface, all that types have to do is provide implementation of methods defined in interface.
// An interface called Human is declared with two methods – Walk and Talk
type Human interface {
Walk(miles int)
Talk()
}
// A type called Employee is declared which would implement the interface Human
// Note that the type does not specify that it would implement the interface
type Employee struct {
name string
designation string
}
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 7
// Implementation of methods of Human interface
func (emp Employee) Walk(miles int){
fmt.Println("I can walk", miles, "miles")
}
func main(){
var emp Human = Employee{name:"Rob Pike", designation:"Architect"}
emp.Walk(10) // I can walk 10 miles
emp.Talk() // I can talk. My name is Rob Pike and I am a Architect
}
The type implementing the interface has to implement all the methods specified in the interface. Also, all the method
signatures should match. If both of these conditions are not matched then compiler complains -
type Contractor struct {
name string
}
Shared Memory
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 8
Actor 1
Asynchronous
message passing
Shared Memory
Asynchronous
Actor 2
message passing
Asynchronous
message passing
Actor 3
fmt.Println("add executed")
fmt.Scanln()
}
Channel
Channels are the conduit through which messages could be sent and received. For sending and receiving messages
using channel <- operator is used.
Channels could be created using make function. While creating the channel the type of message to be passed using
the channel is specified –
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 9
// Declare and allocate a channel
channel := make(chan string)
The below example creates two goroutines – one of them behaves as message sender and the other is message
receiver. The message sender then sends the message via a channel and the message receiver receives the message.
func main(){
channel := make(chan string)
go SendMessage(channel)
go ReceiveMessage(channel)
fmt.Scanln();
}
Message Channel
Message
Acknowledgement
Acknowledgement Channel
func main() {
// Channel used to send the message from sender to receiver
msgChannel := make(chan string)
// Channel used to acknowledge the message receipt by receiver
ackChannel := make(chan string)
go SendMessage(msgChannel, ackChannel)
go ReceiveMessage(msgChannel, ackChannel)
fmt.Scanln()
}
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 10
// goroutine that sends the message
func SendMessage(msgChannel chan string, ackChannel chan string) {
for {
// Send the message to message channel
msgChannel <- "Sending message @" + time.Now().String()
time.Sleep(2 * time.Second)
// Receive the acknowledgement from acknowledgement channel
ack := <-ackChannel
fmt.Println(ack)
}
}
Pointers
Unlike some of the new programming languages such as
Java, C#, Ruby, Python and Swift, Go supports pointers.
Pointers in Go behave much the same way as C and C++.
Pointers reference a location in memory where a value is
stored rather than the value itself.
A pointer is represented using the * (asterisk) character Go is one of the most advanced programming
followed by the type of the stored value. The & (ampersand)
operator is used to denote the address of a variable. In the languages for a reason - it supports the
below example, a pointer is declared that points to an use of pointers.
existing string variable (city). The address of the pointer and
the value it’s pointing to is printed.
var city string = "Bangalore"
// Print the address of the pointer and value the pointer is pointing to
fmt.Println("&ptrToCity = ", &ptrToCity); // prints the address of the pointer
fmt.Println("*ptrToCity = ", *ptrToCity); // prints the value it’s pointing to
Changing the value of the pointer changes the value of the original value it is pointing to -
var city string = "Bangalore"
var ctr int = 10
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 11
// Change the values of the pointers
*ptrToCity = "Kolkata"
*ptrToCtr = 20
Other highlights of Go
1. Go is statically typed 6. Does not support overloading of methods
2. Natively compiled, but garbage collected and operators
3. Go is statically typed with optional type inference 7. Go compiler compiles source code very fast.
4. Supports Defer, Panic and Recover for error handling 8. Does not support Generics
5. Does not support classes and inheritance
Conclusion
Go has evolved as a major programming language suitable for not only system programming, but also for cloud
workloads. Go is supported by multiple cloud platforms, including Google Cloud Platform (App Engine), Cloud
Foundry (including Pivotal Web Service), Heroku and AWS Elastic Beanstalk. Microsoft also added "experimental
support" for Go into its Azure cloud service. Go is becoming a popular language of choice for system programming,
web development, large scale distributed systems and cloud workloads.
Further Reading
• Less is exponentially more: A blog by Rob Pike - http://commandcenter.blogspot.de/2012/06/less-is-exponentially-more.html
• Go website - https://golang.org
• Go FAQ - https://golang.org/doc/faq#Is_Go_an_object-oriented_language
• Go By Example - https://gobyexample.com/
• An Introduction to Programming in Go - https://www.golang-book.com/books/intro
• Little Go Book - http://openmymind.net/assets/go/go.pdf
• Effective Go - https://golang.org/doc/effective_go.html
• Communicating Sequential Processes - https://en.wikipedia.org/wiki/Communicating_sequential_processes
Copyright ©: The brand names mentioned in this POV (Go, D, Rust, C, C++, AWS, Heroku, Google Cloud Platform,
Cloud Foundry, Pivotal Web Service, Azure etc.) belong to their respective owners.
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 12
Aniruddha Chakrabarti
Associate Vice President, Digital, Mphasis
Aniruddha has 16+ years of IT experience spread across systems integration,
technology consulting, IT outsourcing and product development. He has
extensive experience of delivery leadership, solution architecture, presales,
technology architecture and program management of large scale distributed
systems.
As AVP, Digital in Mphasis, Aniruddha is responsible for Presales, Solutions,
RFP/RFI and Capability Development of Digital Practice. Before as Sr.
Manager & Sr. Principal Architect in Accenture, he was responsible for
presales, architecture and leading large delivery teams. He had played
delivery leadership and architecture focused roles in Microsoft, Target, Misys
and Cognizant.
His interests include digital, cloud, mobility, IoT, cognitive computing, NLP,
distributed systems, web, open source, .NET, Java, programming languages
and NoSQL. His industry experience spans Retail, Healthcare, Capital Markets,
Insurance, Travel, Hospitality, Pharma and Medical Technology.
About Mphasis
Mphasis is a global Technology Services and Solutions company specializing in the areas of Digital and Governance, Risk & Compliance. Our solution
focus and superior human capital propels our partnership with large enterprise customers in their Digital Transformation journeys and with global
financial institutions in the conception and execution of their Governance, Risk and Compliance Strategies. We focus on next generation technologies for
differentiated solutions delivering optimized operations for clients.
www.mphasis.com
Copyright © Mphasis Corporation. All rights reserved.
Use Go for System Programming, Distributed Systems and Cloud Workloads Mphasis 13