The next era language

Golang
Do less, Enable more

About Saad Abbasi







I ❤️



Mentor
Techkaro
NgGirls
Dawood UET
Speaker
JS Dive
JS Meetup
React KHI

What this talk is not about
- Hands-on Golang
-
Deep dive into specific topic of Golang or its good practices
- Comparison to other backend technologies like Java, Python and Node.js (if that counts 😄)
- About how fast it is
History of golang
- It's a Google product
- Product was started in 2007
- Officially announced in 2009
- Developed by Googlers ( Rob Pike, Robert Griesemer and Ken Thompson) in 2009
- Google was having hard time keeping up with productivity and performance

In a recent study published by HackerRank of over 70 thousand developers, 37.2% indicated that Go is a language they wish to learn in 2019. No other language scored as high
One of the goals of the Go project were to eliminate the slowness and clumsiness of software development at Google. The language was designed by and for people who write and read and debug and maintain large software systems.
– Rob Pike, Creator of Golang –
Why GoLang, What it solves
- Statically typed compiled language
- Powerful like C++ and fun to write like Python
- Minimal and super easy to learn
- Loose paradigm, tiny bit of OO and FP
- Complies directly to machine language as per cpu arch on any OS
- 3Es
- Efficient compilation
- Efficient execution
- Ease of programming
Golang was originally build to solve Google's problems
And has big Problems!!!

hoW big problems?
- Hardware is big and the software is big
- There are many millions of lines of software
- Servers mostly in C++ and lots of Java and Python
- Thousands of engineers work on the code
- And of course, all this software runs on zillions of machines.
Companies using golang
- Google
- The service that runs dl.google.com--the source for Chrome, Earth, Android SDK, and other large Google downloads--has been rewritten in Go.
- Uber
- Geobase one of the most recent Uber services written in Golang. It matches riders to drivers, sharding the matching across machines.
- Medium
- The online content publishing platform Medium is also using Golang extensively. Their Neo4j database is managed by a service written in Go which they refer to as GoSocial.
- Docker
- Docker is known for its services that's the primary reason to select Golang for docker development.
Unique things about GoLang
- Only 25 reserved keywords
- Go does not encourage long variable names
- Linter is builtin (no more fights)
- Go fast without compromising performance
- 2 weeks are enough to learn it
- No multiple standards, breaking versions previously and in near future
- _ to ignore a return value
- Actual (not encapsulated) multiple return values

reserved keywords
naming convention
File name
- The main entry point file of the application should be named as main.go or same as the application. For example, the entry point file of Gogs is named gogs.go.
Functions and Methods
-
If the main purpose of functions or methods is returning a bool type value, the name of function or method should starts with Has, Is, Can or Allow, etc.
naming convention
Constants
-
Constant should use all capital letters and use underscore _ to separate words.
Variables
-
A variable name should follow general English expression or shorthand.
-
In relatively simple (less objects and more specific) context, variable name can use simplified form as follows:
- user to u
- userID to uid
-
If variable type is bool, its name should start with Has, Is, Can or Allow, etc.
gofmt vs golint
Gofmt reformats Go source code, whereas golint prints out style mistakes.
GoFMT:
To check files for unnecessary parentheses:
gofmt -r '(a) -> a' -l *.go
To remove the parentheses:
gofmt -r '(a) -> a' -w *.go
To convert the package tree from explicit slice upper bounds to implicit ones:
gofmt -r 'α[β:len(α)] -> α[β:]' -w $GOROOT/src
GoLint:
Installation:
go get -u golang.org/x/lint/golint
Usage:
golint ./../main.go
Package management
- Golang module
- 3rd party dependencies
- Directory structure
GOLANG MODULE
Create a new, empty directory somewhere outside $GOPATH/src, cd into that directory, and then create a new source file, hello.go:
package hello
func Hello() string {
return "Hello, world."
}
At this point, the directory contains a package, but not a module, because there is no go.mod file.
Let's make the current directory the root of a module by using go mod init
$ go mod init example.com/hello
go: creating new go.mod: module example.com/hello
3rd party dependencies
The go get tool will find the package, on GitHub in this case, and install it into your $GOPATH.
go get github.com/gobuffalo/flect
Packages are often being updated by the original authors to address bugs or add new features. When this happens, you may want to use the latest version of that package to take advantage of the new features or resolved bug. To update a package, you can use the -u flag with the go get command:
go get -u github.com/gobuffalo/flect
directory structure
Loose Paradigm
- Mostly Procedural (and imperative)
- Some bits of Object-Oriented
- Some bits of functional programming
object oriented go
- No classes
- Nothing like trees of objects
- Still have inheritance and polymorphism


functional go
Concurrency in golang
Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.”
— Rob Pike
single core, Single Thread
Process 1
Thread 1
Thread 2
10
20
30
40
50
60
70
80
90
100
110
120
130
Task 1
Task 2
Dual core, multi Thread
Process 1
Thread 1
Thread 2
10
20
30
40
50
60
70
80
90
100
110
120
130
Process 2
Thread 1
Thread 2
Thread 3
single thread, Multiple goroutines
Thread 1
10
20
30
40
70
Process 1
1
3
2
4
2
3
1
4
4
3
1
2
goroutine
thread
- Go's runtime deal with Operating System (OS) threads
- That execution context that maps to OS thread for concurrency is called goroutine
- Go implements its own scheduler that allows to run many goroutines same OS thread
- Not 1:1 (User to OS) thread ratio
- Goroutines (unlike threads) are transparent to OS
- Goal of concurrency is not parallelism but structural execution
how go manages concurrency
Dynamic stack sizing
- Unlike typical languages Golang has dynamic stack size
- Grows and shrink on memory allocation
- Initial stack size is 4KB
- Roughly quarter of a million goroutines per gigabyte of RAM
how do they communicate?
- Go uses the good concepts of Communicating Sequential Processes (CSP)
- A paper published by Tony Hoare in 1978
- Processes should not communicate by sharing memory
- Processes should share memory by communicating
- Process should share less of memory as address to avoid race conditions
- Go has this concept called "Channels"
goroutine
Channels
channel
- Goroutine-safe
- Channels can pass by values as well as reference
- Channels are buffered and unbuffered
- Channel's buffer works in FIFO semantics
// buffered
ch := make(chan Task, 3);
// unbuffered
ch := make(chan Task);Language of serverless
- Utilizing hardware to its fullest potential makes it a good choice for serverless
- Go has first class support on Lambda, GCFn and ABCFn
- 1.x version support on all clouds

small Cold starts

evaluation
- Job Market
- Community
- Web Assembly
- Google's Product
Questions ???

Golang Talk
By Saad Abbasi
Golang Talk
Talk covers: Concurrency a little about language paradigm, concurrency model, goroutines, channels, serverless and more about language itself.
- 313