GoLang Overview
GoLang Overview
Go Programming
Language
Origin
• Developed ~2007 at Google by
Robert Griesemer
Rob Pike
Ken Thompson
Outline
• Need for a new language
• What is new and better in this language
• Learning Curve?
• Where it is being used and who is using it.
• Current state and open source community
Overview
• At Google there are many many millions of lines of software
on servers mostly in C++ and lots of Java and Python for the
other pieces.
• All this software runs on zillions of machines, which are
treated as a modest number of independent, networked
compute clusters.
• Thousands of engineers work on the code, at the "head" of a
single tree comprising all the software, so from day to day
there are significant changes to all levels of the tree.
Overview
• Development at Google is big, can be slow, and is often
clumsy.
• The goals of the Go project were to eliminate the
slowness and clumsiness of software development at
Google, and thereby to make the process more
productive and scalable.
• The language was designed by and for people who write—
and read and debug and maintain—large software
systems.
Pain Points
Go does have address the issues that make large-scale software
development difficult. These issues include:
• slow builds
• uncontrolled dependencies
• each programmer using a different subset of the language
• poor program understanding (code hard to read, poorly documented, and so on)
• duplication of effort
• cost of updates
• version skew
• difficulty of writing automatic tools
• cross-language builds
A larger view of software engineering is required, and in the design of Go it
has been tried to focus on solutions to these problems.
Dependencies Management
• A file contained about two thousand files that, if simply concatenated
together, totaled 4.2 megabytes. By the time the #includes had been
expanded, over 8 gigabytes were being delivered to the input of the
compiler, a blow-up of 2000 bytes for every C++ source byte.
func main() {
go f(0)
var input string
fmt.Scanln(&input)
}
Goroutines
• Goroutines are lightweight and we can easily create thousands of
them.
By adding ponger…
The program will now take turns printing “ping” and
“pong”.
Channel Directions
Channel Direction
• We can specify a direction on a channel type thus restricting it to either
sending or receiving.
Send-only channels func pinger(c chan<- string).
Attempting to receive from c will result in a compiler error.
Bi-directional func pinger(c chan string).
• This design can have a better performance as well as reducing the number of items
known to the collector. At scale it can make a significant difference.
Composition not inheritance
• Go takes an unusual approach to object-oriented programming,
allowing methods on any type, not just classes, but without any form
of type-based inheritance like sub-classing.
• Instead, Go has interfaces
• A set of methods, no constants
Example:
No Implements Declaration
• All data types that implement these methods satisfy this interface
implicitly; there is no implements declaration.
• Interface satisfaction is statically checked at compile time so despite
this decoupling interfaces are type-safe.
• All data types that implement all methods satisfy interface implicitly.
A datatype can satisfy more than one interface.
Errors
• Go does not have an exception facility in the conventional sense, that
is, there is no control structure associated with error handling.
• A pair of built-in functions called panic and recover allow the
programmer to protect against such things. (rarely used, and not
integrated into the library)
• For error handling is a pre-defined interface
• open source
• compiled, statically typed
• very fast compilation
• C-like syntax
• garbage collection
• built-in concurrency
• no classes or type inheritance or overloading or generics
• unusual interface mechanism instead of inheritance
Where it is today?
• On September 25, 2007, Discussion to create a new programming
language.
• November 10, 2009 Twi compilers created leading to the creation of
an open-source release.
• March 28, 2012 release of GO 1.
Releases
go1 (released 2012/03/28)
go1.1 (released 2013/05/13)
go1.2 (released 2013/12/01)
go1.3 (released 2014/06/18)
go1.4 (released 2014/12/10)
go1.5 (released 2015/08/19) ( Compiler translated from C to GO)
go1.6 (released 2016/02/17)
go1.7 (released 2016/08/15)
go1.8 (released 2017/02/16)
go1.9 (released 2017/08/24)
go1.10 (released 2018/02/16)
go1.11 (released 2018/08/24)
Question?
• In which language Java language is written?
Code Base
• Standard Libraries
• 147 packages
Examples:
math, syslog, io, Plugins, tars, zip, drivers, sql, csv, json, xml, errors etc etc
• Sub-repositories
These packages are part of the Go Project but outside the main Go tree
• 17 Packages
Example:
image, text, time, tools
What is it?
• Nothing new?
• Difficult to write code
• Only one way to write code
• On For statement ( No While/Until)
New Language
• Whenever a new language or new version of a new language is
release, we see:?
• As
• Asa
• Asa
• Sas
• Sas
No Generics
• No Classes
• No Inheritence
• No constructors.
• No annotations.
• No generics.
• No exceptions.
Need?
•Simplicity
•Safety
•Readability
•Efficient/Speed
• Nothing New
Where is Go language is being
used?
• Go runs directly on underlying hardware. One most considerable
benefit of using C, C++ over other modern higher level languages like
Java/Python is their performance. Because C/C++ are compiled and
not interpreted. Processors understand binaries.
Top 5 editors
• LiteIDE
• Visual Studio Code
• Eclipse with GoClipse
• Atom IDE
• Vim
• https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins
C++ vs Go
Simplicity
C++ GO
Semi colon at the end of line
a = b; No Semicolon
a=b
No () parentheses with if and for
If (a >2){ …} If a > 2 …
Go performs pointer escape analysis. If the pointer escapes the local stack, which it does in this case, the object is allocated
on the heap. If it doesn't escape the local function, the compiler is free to allocate it on the stack
New in Go Language
• Closures: You can write functions inside functions and you can return functions just like in a
functional language.
package main
import "fmt"
For instance:
a, b := getThings()
New in Go Language
• Arrays, Slices and Map
A slice is a segment of an array. Like arrays slices are indexable and have a
length. Unlike arrays this length is allowed to change.
var x []float64
or
x := make([]float64, 5, 10)
10 represents the capacity of the underlying array which the slice points to:
or
arr := [5]float64{1,2,3,4,5}
x := arr[3:5]
New in Go Language
• Map
A map is an unordered collection of key-value pairs.
var x map[string]int
x := make(map[string]int). ////Length is 0
x["key"] = 10 // length is 1
x["key"] = 10 // length is 2
fmt.Println(x)
delete(x, 1)
• GO is not a replacement for Java/C/(C++?) even on a language level.
• Java and Go are not supposed to serve the same type of tasks –
• Java is enterprise development language,
• Go is a system programming language.
Future
• The new darwin/arm64 port and external linking features fuel the
• Go mobile project, an experiment to see how Go might be used for
building apps on Android and iOS devices. (The Go mobile work itself
is not part of this release.)
Companies using Go
Many other companies use Go as well; the list is very long, but a few of the
better known are:
• Uber
• Novartis
• Basecamp
• BBC Worldwide
• Canonical
• Heroku
• Nokia
• SoundCloud