Go programming language

appstud 203 views 40 slides Apr 09, 2019
Slide 1
Slide 1 of 40
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40

About This Presentation

Après la présentation de #Flutter, Julien nous révèle toute la puissance de #Go, encore un autre langage de programmation créé par Google.

After the #Flutter presentation, Julien reveals all the power of #Go, yet another programming language created by Google.


Slide Content

Go

A modern programming language for modern times

by Julien Galletta

(ei
2
=
i
=
=
©
a
©
2
je

Mandatory hello world

package main
import "fmt"
func main() {

fmt.Println("Hello world!")
}

History

Designed at Google in 2007

Publicly announced in November 2009, version 1.0 was released in
March 2012

Version 1.12 released on February 2019

Version 2.0 in discussion

Creators

+ Robert Griesemer (Java HotSpot virtual machine, V8 Js engine)
+ Rob Pike (wrote the first window system for Unix, worked on UTF-8)

+ Ken Thompson (implemented the original Unix operating system,
invented the B programming language, worked on UTF-8)

Why

« Computing landscape today is almost unrelated to the environment in
which the languages being used, mostly C++, Java, and Python, had
been created: multicore processors, networked systems, massive
computation clusters, ...

« Scale has changed: today’s server programs comprise tens of millions
of lines of code, are worked on by hundreds or even thousands of
programmers, and are updated literally every day

* Build times, even on large compilation clusters, have stretched to
many minutes, even hours

Guiding principles

Go must solve these problems

+ Go must work at scale, for large teams of programmers working
on them, for programs with large numbers of dependencies

« Go must be familiar, roughly C-like. Google needs to get
programmers productive quickly in Go, means that the
language cannot be too radical

+ Go must be modern. It should have features like concurrency
so that programs can make efficient use of multi core machines.
It should have built-in networking and web server libraries so
that it aids modern development

Software engineering is what happens to programming when you add
time and other programmers.

— Russ Cox

+ Software programming: program you write for yourself
* Software engineering: product that many people will
work on over time

Engineers will come and go, teams will grow and shrink,
requirements will change, features will be added and bugs fixed.

Simplicit
piety requisite for

á — Edsger W. Dijkstra
+ "| can't understand this 9 y

code”

* You're scared to make a
change because you're
worried it'll break another
part of the program

« Apart you don't
understand and don't
know how to fix

There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the other
way is to make it so complicated that there are no obvious deficiencies.

The first method is far more difficult.
—C.A.R. Hoare

Complexity turns reliable software in unreliable software.
Complexity is what kills software projects.
Therefore simplicity is the highest goal of Go.

Whatever programs we write, we should be able to agree
that they are simple.

Readability

We should strive for
readability

Readability is important
because all software is
written by humans to be read
by other humans.

Code is read many more
times than it is written. A
piece of code will, over its
lifetime, be read hundreds,
thousands of times.

ssential for

— Mark Reinhold

t be written for people
only incidentally for
machines to execute.

Hal Abelson and Gerald Sussman

The most important skill for a programmer is the ability to effectively

communicate ideas.
— Gastón Jorquera

Ifyou can't understand what a program is doing, how can
you hope to maintain it?

A piece of software that more than one person will
contribute to, or that will be used by people over a long
enough time that requirements or features changes, then
your goal must be for your program to be maintainable.

The first step towards writing maintainable code is making
sure the code is readable.

Use the bloody brackets for fuck's sake!

t of arranging code

Productivity
d be changeable

— Sandi Metz

How much time do you
spend doing useful work
versus waiting for your tools,
or hopelessly lost in a foreign
code-base?

Go was designed while waiting for a C++ program to compile.

— Go developer humour

Fast compilation is a key feature of Go.
Compilations which take minutes in other languages, take
seconds in Go

Go programmers realise that code is written to be read and
so place the act of reading code above the act of writing it.

« Tooling enforces that all code be formatted in a specific
style

Go programmers don’t spend days debugging inscrutable
compile errors. They don't waste days with complicated
build scripts or deploying code to production. And most
importantly they don’t spend their time trying to understand
what their coworker wrote.

Productivity is what the Go team mean when they say the
language must scale.

n
©
2
2
©
©
LL

Open source

« Free BSD license

+ httos://github.com/golang/go

+ Main contributor is the Google team

E

Clean syntax

110

82,5
Number of

reserved
words in

languages

C Python Js Java Rust Dart Kotlin Swift C++

a

2

Statically typed

+ Static typing and run-time efficiency (like C++ or Java)
« Non-strict typing (type inference)

// Initialised with 0 value
var str string

// Type inference
var i = 42

// Short syntax
isJeanAFanboy := true

Garbage collected

« Tricolor garbage collection
algorithm

* Optimized for latency over
throughput

Built in support for concurrency

Concurrency is about dealing with lots of things at once. Parallelism
is about doing lots of things at once.

Goroutines

+ Lightweight thread of execution

* Go runtime multiplexes those goroutines over operating
system threads

+ Multiple goroutines can run concurrently on a single
OS thread

No classes
No inheritance
No constructors
No annotations
No generics
No exceptions

Besausetuskyeu

| Peculiar object-oriented model

* Composition only
* Structs types with methods

| Interface system

+ Enable loosely coupled systems

+ Simply defined as set of functions

+ Any type which implements those functions implicitly
implements the interface

No exceptions, handle errors yourself

» Forces developers to handle basic errors (no more
TODO we should handle this, probably)
* Classic go code:

beer, err grabBeer('IPA')
nil {
logge ('no more beer')

return err

}

// Use the beer

Great built-in libraries

+ net/http: Provides HTTP client and server
implementations

+ database/sql: For interaction with SQL databases

« encoding/json: JSON is treated as a first class
member of the standard language

+ html/templates: HTML templating library

* jo/utils: Implements I/O utility functions

« Testing: Automated testing

Good tooling

* GoFMT: automatically formats and indents your code
« Go run: compiles your code and runs it
* Godoc: equivalent of javadoc

Weaknesses

Lack of generics

Cons
+ Would complexifies the language
« Increases execution or compilation time
+ Often mis-used or over-used (needless abstraction)

Pros

+ Useful to avoid code duplication
« Avoid hacks with the Interface (“Any”) type

In discussion for go 2

err := doSomething()

if err != nil {
Tedious error handling // handle the error here
D
* Lot of repetition func doSomething() error {
+ No typed error in standard library err := someMethod()

if err != nil {
return err

}
err = someOther()
if err != nil {

In discussion for go 2 return err

someOtherMethod()

No enumeration type

Workarounds are verbose

GOPATH

Before v1.11 you had to place all your go projects in the same
repository that was the same one used to checkout libs from
GitHub

Dependency management

+ Dependencies management was not part of the language before
v1.11

+ Third-Party lips were available, the main one being dep

« Dep was used by a large number of people, but Go main
maintainers decided to go with a completely new implementation
they unveiled out of the blue (not cool.)

Not very good for functional programming

+ No clone function

+ Pointers and references

« Lacks immutability for all but a few (native) types
+ Arrays and slices shares the same memory space

E
©
a
=
©
=
Q
6)

A
ö

Fast, fun for humans

0

Fast, efficient for computers

00

@
e

- "Gets the job done” type of language

+ Easy to learn

+ Fun despite its limited C-like syntax

+ Focuses on readability and maintainability
- Excels in concurrency and speed

+ Has a lot of room for growth and improvement

https://medium.com/@kevalpatel2106/why-should-you-learn-go-
1607681fad65

https: //hackernoon.com/the-beauty-of-go-98057e3f0a7d
https://talks.golang.org/2012/splash.article

https://dave.cheney.net/practical-go/presentations/qcon-
china.html#_choose_identifiers_for_clarity_not_brevity