0% found this document useful (0 votes)
2 views45 pages

GoLang Overview

Go is a programming language developed at Google to improve software development efficiency and scalability, addressing common pain points like slow builds and uncontrolled dependencies. It features built-in concurrency, garbage collection, and a unique interface mechanism instead of traditional inheritance. Go is widely used in various companies, including Uber and BBC Worldwide, and has a growing open-source community.

Uploaded by

Abu Haque
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views45 pages

GoLang Overview

Go is a programming language developed at Google to improve software development efficiency and scalability, addressing common pain points like slow builds and uncontrolled dependencies. It features built-in concurrency, garbage collection, and a unique interface mechanism instead of traditional inheritance. Go is widely used in various companies, including Uber and BBC Worldwide, and has a growing open-source community.

Uploaded by

Abu Haque
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Overview of

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.

• Java : There are certain tools (JBOSS TattleTale ) can be used to


identify unused jars at compile time. Then for each of the jar in that
list, one has to search in the project to find if the highest package
level of the jar was used anywhere .
• Still there are many jars are used to just at compile time.
GO way to controlling unused
imports
Unused dependencies are a compile-time error (not a warning,
an error).
Bigger Changes
There are some much bigger changes too, stepping far from the
traditional C, C++, and even Java models. These include linguistic
support for:
• concurrency
• garbage collection
• interface types
• reflection
• type switches
Concurrency
Concurrency is important to the modern computing environment with
its multicore machines running web servers with multiple clients, what
might be called the typical Google program.
This kind of software is not especially well served by C++ or Java, which
lack sufficient concurrency support at the language level.
Concurrency
• Go enables simple, safe concurrent programming but does
not forbid bad programming.
• The motto is, "Don't communicate by sharing memory, share memory
by communicating."
Goroutines
• A goroutine is a function that is capable of running
concurrently with other functions.
package main
import "fmt"

func f(n int) {


for i := 0; i < 10000; i++ {
fmt.Println(n, ":", i)
} go followed by a function invocation
}

func main() {
go f(0)
var input string
fmt.Scanln(&input)
}
Goroutines
• Goroutines are lightweight and we can easily create thousands of
them.

go followed by a function invocation


Channels
• Channels provide a way for two goroutines to communicate with one
another and synchronize their execution.

keyword chan followed by the type of the


things that are passed on the channel

Using a channel like this synchronizes the two


goroutines. When pinger attempts to send a
message on the channel it will wait until printer is
ready to receive the message. (this is known as
blocking)
Channels

keyword chan followed by the type of the


things that are passed on the channel

Using a channel like this synchronizes the two


goroutines. When pinger attempts to send a
message on the channel it will wait until printer is
ready to receive the message. (this is known as
blocking)

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).

A channel that doesn't have these restrictions is known as bi-directional.


A bi-directional channel can be passed to a function that takes send-only or
receive-only channels, but the reverse is not true.
Garbage collection
• Go has no explicit memory-freeing operation: the only way allocated
memory returns to the pool is through the garbage collector.
• Experience with Java in particular as a server language has made
some people nervous about garbage collection in a user-facing
system.
• The overheads are uncontrollable, latencies can be large, and much
parameter tuning is required for good performance.
• Go, however, is different. Properties of the language mitigate some
of these concerns. Not all of them of course, but some.
Layout of data structures in Go
Consider this simple type definition of a data structure containing a buffer (array) of
bytes:
type X struct {
a, b, c int
buf [256]byte
}
In Java, the buf field would require a second allocation and accesses to it a second level
of indirection.
In Go, however, the buffer is allocated in a single block of memory along with the
containing struct and no indirection is required.

• 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 …

for (i := 0; i < 100; i++){ for i := 0; i < 100; i++ {


… …
} }

Types after names


Foo foo = 2; var foo Foo = 2
C++ vs Go
Simplicity
C++ GO
No Implicit Conversion
(also applies to comparison via == and !=)
Compiler error
int a = -2;
var a int = -2
uint c = a;
var b uint = a
float64 d = 1.345;
var d float64 = 1.345
int e = ‘c’;
var e int = `c`
No Enums
Only for loops
for i := 0; i < 100; i++ {…. }
...

for keepGoing {….}


...

for i, c := range things {…}


C++ vs Goc
Simplicity
C++ GO
Arrays - Slices
Compiler error
int a = -2;
var a int = -2
uint c = a;
var b uint = a
float64 d = 1.345;
var d float64 = 1.345
int e = ‘c’;
var e int = `c`
No Enums
Only for loops
for i := 0; i < 100; i++ {…. }
...

for keepGoing {….}


...

for i, c := range things {…}


New in Go Language
• A function can return a reference to a local variable (declared in a method).
package main
import "fmt"

type Record struct {


i int
}
func returnLocalVariableAddress() *Record {
return &Record{1} // a new record is allocated on the stack
}
func main() {
r := returnLocalVariableAddress()
fmt.Printf("%d", r.i)
}

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"

func CounterFactory(j int) func() int {


i := j
return func() int {
i++
return i
}
}
func main() {
r := CounterFactory(13)
fmt.Printf("%d\n", r())
fmt.Printf("%d\n", r())
fmt.Printf("%d\n", r())
}
New in Go Language
• Function can have multiple return types

For instance:

func getThings() (int, Foo) {


return 2, getFoo()
}

a, b := getThings()
New in Go Language
• Arrays, Slices and Map

Array is a fixed length array. var x [5]int

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

You might also like