4. Creators
Created by Google Inc in 2009 by:
● Ken Thompson (B, C, UNIX, UTF-8),
● Rob Pike(UNIX, UTF-8) and
● Robert Griesmer (HotSpot, JVM)
● Procedural and imperative
● Object-oriented - Yes and No
● Compiled
● Syntax similar to C-family
● Concurrency inspired by CSP
Nature
5. Why they created Go?
● Frustration with existing language
● Had to choose either efficient compilation, efficient execution or ease of
programming
● Dynamic typed (Python,JavaScript): easy +ve, safety -ve, efficiency -ve
● Compiled ( C ) are fast, but difficult to work and not safe
● Interpreted (Ruby) safe but slower
● Libraries and tools cannot address issues like lightweight type system,
concurrency and garbage collection, rigid dependency specification and
so on
6. Go removes all obstacles
Fast: fast compilation like interpreted language
Safe: Strongly and statically typed and garbage collected
Easy: Concise , easy to read
Modern: Built in support for multi-core networked distributed applications
9. GO GO GO...
● Overcoming hardware limitations
● Go’s Goroutines
● Go runs directly on underlying hardware
● Easy to maintain
● Best for microservices
10. Overcoming hardware limitations
● 2000 - 1st P-4 processor with 1.4 GHz clock speed,
● 2004 - with 3.0GHz clock speed
● 2014 - Core i7 4790K with 4.0Ghz clock speed
● Manufacturers added multiple cores, hyper-threading technology, more
cache
● Limitations on adding more h/w, cannot rely on hardware for
performance
● Multi-core processors run multiple threads simultaneously - Go captures
concurrency
11.
12.
13. Go’s Goroutines
● Today's applications use multiple microservices which needs easy
concurrency and scalability with increase no of cores
● Modern prog lang (like Java,Python etc) - 90’s single threaded
environment
● They support multi-threading but problems with concurrent execution,
threading-locking, race conditions and deadlocks
● Creating new thread in Java is not memory efficient, 1 thread = 1mb
memory of heap size
● Golang created in 2009 under multiple core processors. 1 thread = 2Kb
14. Go’s Goroutines
● Growable segmented stacks - use more memory only when needed.
● Faster startup time than threads
● Come with built-in primitives to communicate safely between themselves
(channels)
● Goroutines and OS threads do not have 1:1 mapping. A single goroutine
can run on multiple threads
16. Go runs directly on underlying hardware
Execution steps for VM based languages Execution steps for compiled languages
17. Easy to maintain
● No classes: Go has only structs instead of classes
● Does not support inheritance: Make code easy to modify. In other
languages like Java/Python, if the class ABC inherits class XYZ and you
make some changes in class XYZ, then that may produce some side
effects in other classes that inherit XYZ. By removing inheritance, Go
makes it easy to understand the code also
● No constructors
● No assertions
● No generics
● No exceptions
19. Best suitable for microservices
● Lightweight, very fast
● Fantastic support for concurrency, which is a powerful capability when
running across several machines and cores
● Microservice framework like go-micro or go-kit
● Pluggable architecture based on RPC - gRPC
21. Trends - % of Stack Overflow question for Go
Source: https://insights.stackoverflow.com/trends?tags=go
22. Trends - Most used and wanted languages in 2017
Source:
https://www.stackoverflowbusiness.com/hubfs/content/2017_Global_Developer_Hiring_Landscape.
pdf
24. Trends - Programming language Hall of Fame
Source: https://www.tiobe.com/tiobe-index/
25.
26. Syntax
C Java Go
Extension .c .java .go
VM Needed Not needed JVM is there to
convert java files
to class files
Not needed
Compiler TurboC javac gc
Program
Execution
Starts from
main() function
Starts from
public static void
main(String[ ]
args)
starts from
func main() and
should be in
package main
27. Syntax
C Java Go
Variable
Declaration
int a; float b;
int a,b,c;
Initialization
int a=25;
char c=’a’;
int a; float b; a int
var c string
var a,b,c float
Initialization
var o,p int=34,65
k=45.67 (type of variable
auto judged by
compiler)
Shorthand notation
a:=30 (only inside
function)
Data Types bytes,short,int,long,
double,float,char,void
Primitive
bytes,short,int,long
,double,float,
boolean,char
uint, int,float32,...,
complex, byte,rune
28. C Java Go
Constant const int LEN=10 final int PI=3.14 const LEN int=10
Operators All operators like
Arithmetic,Logical,
Bitwise etc. are
same
All operators like
Arithmetic,Logical,
Bitwise etc. are
same
All operators like
Arithmetic,Logical,
Bitwise etc. are
same
Decision Making
Statements
(If else)
if(i>5){
flag=true
}
else{
flag=false
}
if(i>5){
flag=true
}
else{
flag=false
}
if i>5{
flag=true
}else{
flag=false
}
Syntax
29. C Java Go
Switch Case switch(i){
case 1: ……
break;
.
.
default:.....
break ;
}
switch(i){
case 1: ……
break;
.
.
default:.....
break ;
}
Expression switch
switch marks{
case 90: grade==’A’
case 50,60,70:grade==’C .
default: fmt.Println(“Invalid”)
}
Type switch
var x interface{}
switch x.(type){
case int:
fmt.Println(“x is int”)
default:
fmt.Println(“I don’t know”)
}
Syntax
30. C Java Go
For loop for(i=1;i<5;i++){
……...
}
for(i=1;i<5;i++){
…………..
}
for i:=0;i<5;i++{
………..
}
for key,val:=range
numbers{
…………..
}
While Loop while(i<10){
i++;
}
while(i<10){
i++;
}
for sum<100{
sum+=sum
}
Syntax
31. C Java Go
Function Declaration
int sum(int,int)
int sum(int x,int y){
……………
return
}
Calling
int add;
add=sum(10,20)
-Supports pass by
value and reference
Define method
public int sum(int
a,int b){
…………..
return
}
-Only pass by value
func sum(no int)
(int){
……
return ..
}
-Both pass by value
and reference
Function
Overloading
No Yes No
Syntax
32. C Java Go
Variadic Functions No printMax(10,20,30)
public static void
printMax(double…
numbers){
……...
}
func sum(num
...int) int{
…..
….
}
Ex. Println() is
implemented in
“fmt”
package
func Println(a
...interface{})(n
int,err error)
String char greet[6]={
‘h’,’e’,’l’,’l’,’o’,’0’
}
String s=new
String(“Hello”)
var
greeting=”Hello”
Syntax
33. C Java Go
String Functions strcpy(str1, str2)
strcat(str1,str2)
strlen(s1)............
str.length()
str.concat(str2)
str.compareTo(str2
)
String is made up
of runes. Rune is
UTF Format (A-
’65’,a-’97’)
len(str)
strings.join(str1,str
2)
Arrays Fixed size data
structure
Declaration
int array[10];
Accessing Elements
int value=array[10]
Declaration
int[ ] numbers
Creation
numbers=new
int[10]
Declaration
var number [10]int
Initialization
Var bal=[10]
int{...............}
Syntax
34. C Java Go
Slice There is no growable
data structure
java.util.Collection
package provides
dynamic growable
array. E.g List,Set,Map
etc
Growable data
structure
Declaration
var numbers []int
Creation
numbers=make([
]int,5,5)
5 len, 5 is capacity
append(),copy()
Default value - nil
Map There is no map data
structure in C
Key, Value pair
Map m=new
HashMap()
Map[key] value
Default value is nil
Syntax
35. C Java Go
Map - m.put(“Zero”,1)
put( )
get( )
ForEach to iterate
over collection and
array
var dictionary=map
[string] int
dictionary[“Zero”]=1
Create using make
var dict=make(map
[string] int)
Range -> to iterate over
slice,map
for key,val:=range values
{
……...
}
Syntax
36. C Java Go
Struct / Class Collection of fields and
methods
struct book{
int id;
char name[50];
char author[50];
}book;
//Access members
struct book book1;
strcpy(book1.id,400)
Java does not have
struct but have
class
class Books{
int id;
String name;
String author;
void display(){
…………...
}
}
Books b=new
Book();
b.name=”Vision”
Go have struct to
hold data in
variables.
type struct Book{
id int;
name string;
}
func(book3 *Book)
display() {
…………..
}
book1:=new(Book)
var book2 Book;
book2.name=”Go”
Syntax
37. C Java Go
Embedding It is like inheritance
in Java
class A {
….
}
Class B extends A
{
...
}
type Person struct{
Name string
}
func (per *Person)
speak(){
……..
}
type Mobile struct{
Person
Model String
}
Mob:=new(Mobile)
mob.Person.speak(
)
Syntax
38. C Java Go
Pointer Direct address of
memory location
int *a
a=&b;
NULL pointer
Always assign null
value to pointer
variable when you
don’t know exact
address to assign
int *a=NULL;
Java does not
support pointer
var a *int
var fp *float32
a=&p
NIL pointer
var *p int=nil
Syntax
39. C Java Go
Interface C does not have
interface concept
-Collection of only abstract
methods (from java 8 it
supports static methods
also)
-Classes implements
interface
public interface Animal{
public void eat();
public void sleep();
}
public class Monkey extends
Animal{
………..
}
-Provides method
signatures
type Shape
interface {
area() float64
}
type square struct{
side float64
}
func(sq Square)area( )
float64 {
return sq.side*sq.side
}
Same method signature
is implemented
Syntax
40. C Java Go
Go has empty interface which is like
object class in java. An empty
interface can hold any type of
value.
fmt.Println(a ...interface{ })
Type Casting Convert variable from
one to another data type
(type) expression
int sum=5,count=2;
Double d=(double)
sum/count
Widening
byte->short->int->float->
long->double
float=10
Narrowing
double->float->long->int-
>short->byte
int a=(float)10.40
var i int=10
var f float64=6.44
fmt.Println(float64(i))
fmt.Println(int(f))
Syntax
41. C Java Go
Error Handling -No direct support for error
handling
But it returns error no.
perror() and strerror() used to
display text message associated
with error no
int dividend=20;
int divisor=0;
int quotient;
if(divisor==0){
fprintf(stderr,”Divide by zero”);
exit(-1);
}
Exception Handling
-Exception is problem
arises during program
execution.
Checked/Unchecked Ex.
try{
……
}
catch(Exception ex){
……..
}
finally{
……..
}
No try/catch mechanism.
Instead it is having
defer,panic,recover
concepts.
-Provides error interface
type error interface{
Error() string
}
Normally function
returns error as last
return value. Use
errors.New() for
specifying error msg.
Syntax
42. C Java Go
Concurrency /
Multithreading
Doesn’t have
support.
Java have multithreading
concept for simultaneous
execution of different
tasks at same time.
We need to extends
Thread class or Runnable
interface and then
implement run( ) and write
your code
start(),run(),join(),sleep(),...
Here, we use
goroutines for
concurrent
execution.
To make function
as goroutine just
append go in front
of your function.
Two goroutines are
communicating
using channel
concept
Syntax
Procedural because procedures (functions) are stitched together to form a program.
Imperative because functions are built out of statements which are generally phrased as imperative commands.
Go takes good of both the worlds ( C family and Erlang). Easy to write concurrent and efficient to manage concurrency.