trivadis techevent 2016 go - the cloud programming language by andija sisko

33
BASLE BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH GO The Cloud Programming Language An Introduction and Feature Overview Andrija Sisko

Upload: trivadis

Post on 23-Jan-2017

26 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

BASLE BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENEVA

HAMBURG COPENHAGEN LAUSANNE MUNICH STUTTGART VIENNA ZURICH

GOThe Cloud Programming Language –

An Introduction and Feature Overview

Andrija Sisko

Page 2: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Agenda

Go - The Cloud Programming Language - An Introduction and Feature Overview2 19.09.2016

1. Background

2. Features

Page 3: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go - The Cloud Programming Language - An Introduction and Feature Overview3 19.09.2016

Background

Page 4: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Brief Histroy

Go - The Cloud Programming Language - An Introduction and Feature Overview4 19.09.2016

Started at Google as an answer to some of the problems seen when developing

software infrastructure at Google

Go's lead developers at Google were Robert Griesemer (Google's V8 JavaScript

engine), Rob Pike (UTF-8, with Ken Thompson), and Ken Thompson (1st

implementer of Unix, with Dennis Ritchie)

Discussions start in late 2007

First draft of specification in March 2008

Open sourced to the public in 2009

Go 1.0, 2012, language specification, standard libraries, and custom tools

Go 1.5, 2015, compiler and runtime written in Go

Go 1.7.1 (latest), 07.09.2016

Page 5: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Origins (1)

Go - The Cloud Programming Language - An Introduction and Feature Overview5 19.09.2016

The Algol family of programming languages

– Block structure

– Nested and recursive functions and procedures

– Type declarations

– Static typing

Algol60 successors: C, Pascal

Pascal successors: Modula, Modula-2, Oberon, Object Oberon, Oberon-2

Page 6: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Origins (2)

Go - The Cloud Programming Language - An Introduction and Feature Overview6 19.09.2016

C: statement and expression syntax

Pascal: declaration syntax

Modula 2, Oberon 2: packages

CSP, Occam, Newsqueak, Limbo, Alef: concurrency

BCPL: the semicolon rule

Smalltalk: methods

Newsqueak: <-, :=

APL: iota

Page 7: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Origins (3)

Go - The Cloud Programming Language - An Introduction and Feature Overview7 19.09.2016

Tree node lookup in Oberon-2

MODULE Trees;

IMPORT Texts, Oberon;

TYPE

Tree* = POINTER TO Node; (* star denotes export, not pointer!

*)

Node* = RECORD

name-: POINTER TO ARRAY OF CHAR; (* minus denotes

read-only export *)

left, right: Tree

END;

PROCEDURE (t: Tree) Lookup* (name: ARRAY OF CHAR): Tree;

VAR p: Tree;

BEGIN p := t;

WHILE (p # NIL) & (name # p.name^) DO

IF name < p.name^ THEN p := p.left ELSE p := p.right END

END;

RETURN p

END Lookup;

...

package trees

import ( "fmt"; "runtime" )

type (

Tree *Node

Node struct {

name string

left, right Tree

})

func (t *Node) Lookup(name string) Tree {

var p Tree

p = t

for p != nil && name != p.name {

if name < p.name { p = p.left } else { p = p.right }

}

return p

}

...

Tree node lookup in Go

Source: The Evolution of Go; GopherCon 2015 Keynote - July 9, 2015; Robert Griesemer; Google, Inc.

Page 8: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Modern Programming Language Design?

Go - The Cloud Programming Language - An Introduction and Feature Overview8 19.09.2016

Object Oriented

Functional

Generics

Memory Management (Garbage Collection)

Concurrency

Page 9: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go Design Goals (1) Simplicity

Go - The Cloud Programming Language - An Introduction and Feature Overview9 19.09.2016

Clean procedural language designed for scalable cloud software.

Composable distinct elements, including:

– concrete data types

– functions and methods

– interfaces

– packages

– concurrency

Good tools, fast builds.

All the pieces feel simple in practice.

Keywords: Go ~25, Java ~50, C# ~80

Source: Simplicity is Complicated; dotGo 09.11.2015; Rob Pike; Google Inc.

Page 10: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go Design Goals (2) Maintainability

Go - The Cloud Programming Language - An Introduction and Feature Overview10 19.09.2016

import scalaz._

import scalaz.std.list._

import scalaz.syntax.monad._

import scalaz.syntax.monoid._

import scalaz.syntax.traverse.{ToFunctorOps => _, _}

class Foo[F[+_] : Monad, A, B](val execute: Foo.Request[A] => F[B], val joins: Foo.Request[A] => B => List[Foo.Request[A]])(implicit J: Foo.Join[A, B]) {

def bar: Foo[({type l[+a]=WriterT[F, Log[A, B], a]})#l, A, B] = {

type TraceW[FF[+_], +AA] = WriterT[FF, Log[A, B], AA]

def execute(request: Request[A]): WriterT[F, Log[A, B], B] =

self.execute(request).liftM[TraceW] :++>> (repr => List(request -> request.response(repr, self.joins(request)(repr))))

----- REDACTED -------

Source: Moving a team from Scala to Golang; http://jimplush.com/talk/2015/12/19/moving-a-team-from-scala-to-golang/

Page 11: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go Design Goals (3) Readability

Go - The Cloud Programming Language - An Introduction and Feature Overview11 19.09.2016

From Docker principles

(https://github.com/docker/docker/blob/master/project/PRINCIPLES.md)

– 50 lines of straightforward, readable code is better than 10 lines of magic that

nobody can understand.

Page 12: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go Characteristics

Go - The Cloud Programming Language - An Introduction and Feature Overview12 19.09.2016

Procedural language

Statically typed

Syntax similar to C (parentheses, no semicolons), with the structure of Oberon-2

Compiles to native code (no VM) fast execution

No classes, but structs with methods

Interfaces

No implementation inheritance (composition over inheritance)

Functions are first class citizens

Functions can return multiple values

Has closures

Pointers, but not pointer arithmetic

Built-in concurrency primitives: Goroutines and Channels

Page 13: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go - The Cloud Programming Language - An Introduction and Feature Overview13 19.09.2016

Features

Page 14: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Fast Compiler

Go - The Cloud Programming Language - An Introduction and Feature Overview14 19.09.2016

Reason: Dependency analysis.

– Go provides a model for software construction that makes dependency analysis easy and

avoids much of the overhead of C-style include files and libraries. (Go FAQ)

– Builds in less than 20 seconds on a

Source: https://www.openhub.net/p/docker

Page 15: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Compilation and Deployment

Go - The Cloud Programming Language - An Introduction and Feature Overview15 19.09.2016

Go usually (if pure Go and not linking code from other languages) compiles to one

single statically linked, native executable.

The deployment of a Go program can be done by simply copy the executable.

Page 16: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Package System (1)

Go - The Cloud Programming Language - An Introduction and Feature Overview16 19.09.2016

All Go source is part of a package.

Every file begins with a package statement.

The imported package path is a simple string.

Programs start in package main.

package main

import "fmt"

func main() {fmt.Println("Hello, world!")

}

Page 17: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Package System (2)

Go - The Cloud Programming Language - An Introduction and Feature Overview17 19.09.2016

Function Println is exported other packages can call it. Starts with a upper case

letter.

Function newPrinter in unexported can only be used inside of the fmt package.

// Package fmt implements formatted I/O.package fmt

// Println formats using the default formats for its// operands and writes to standard output.func Println(a ...interface{}) (n int, err error) {

...}

func newPrinter() *pp {...

}

Page 18: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Testing Go Packages

Go - The Cloud Programming Language - An Introduction and Feature Overview18 19.09.2016

Package testing provides support for automated testing of Go packages.

Tests are distinguished by file name. Test files end in _test.go.

package fmt

import "testing"

var fmtTests = []fmtTest{{"%d", 12345, "12345"},{"%v", 12345, "12345"},{"%t", true, "true"},

}

func TestSprintf(t *testing.T) {for _, tt := range fmtTests {

if s := Sprintf(tt.fmt, tt.val); s != tt.out {t.Errorf("...")

}}

}

Page 19: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Code Organization

Go - The Cloud Programming Language - An Introduction and Feature Overview19 19.09.2016

Go code is kept in a workspace.

The GOPATH environment variable tells the Go tool where your workspace is

located.

Using a file layout for builds means no configuration: No makefile, no build.xml, no …

Everyone in the community uses the same layout. This makes it easier to share

code.

$GOPATH/src/

github.com/user/repo/mypkg/

mysrc1.gomysrc2.go

cmd/mycmd/main.go

bin/mycmd

Page 20: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Remote Packages

Go - The Cloud Programming Language - An Introduction and Feature Overview20 19.09.2016

The import path can refer to remote repositories.

$ go get github.com/4ad/doozer // Shell command to fetch package

import "github.com/4ad/doozer" // Doozer client's import statement

var client doozer.Conn // Client's use of package

$ go get -u all // Updates all packages under the $GOPATH

Page 21: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Error Handling

Go - The Cloud Programming Language - An Introduction and Feature Overview21 19.09.2016

Go has no exceptions.

Errors are indicated with the built-in Error type.

The idiomatic way of error handling in go is to check for errors right after the function

call.

f, err := os.Open("filename.ext")if err != nil {

log.Fatal(err)}// do something with the open *File f

Page 22: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Defer Statement

Go - The Cloud Programming Language - An Introduction and Feature Overview22 19.09.2016

A defer statement pushes a function call onto a list. The list of saved calls is

executed after the surrounding function returns.

Can be used to put acquisition and cleanup of a resource side by side

func CopyFile(dstName, srcName string) (written int64, err error) {src, err := os.Open(srcName)if err != nil {

return}defer src.Close()

dst, err := os.Create(dstName)if err != nil {

return}defer dst.Close()

return io.Copy(dst, src)}

Page 23: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Methods on Structs

Go - The Cloud Programming Language - An Introduction and Feature Overview23 19.09.2016

package main

import "fmt"

type Rectangle struct {length, width int

}

func (r Rectangle) Area() int {return r.length * r.width

}

func main() {r1 := Rectangle{4, 3}fmt.Println("Rectangle is: ", r1)fmt.Println("Rectangle area is: ", r1.Area())

}

Page 24: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Tools

Go - The Cloud Programming Language - An Introduction and Feature Overview24 19.09.2016

go : build, clean, run, test, install, get, … .

go vet : Go linter for analysing Go code and finding common mistakes.

golint : Checks the code for style violations.

go tool cover : Reports code coverage (untested code).

godoc : Extracts and generates documentation for Go programs.

gofmt : Formats the code according to the only acceptable way of formatting it.

and many more …

Page 25: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Interfaces

Go - The Cloud Programming Language - An Introduction and Feature Overview25 19.09.2016

Interfaces are named collections of method signatures.

Whether or not a type satisfies an interface is determined automatically. Not

implements keyword.

Any type that defines this method is said to satisfy the Stringer interface

type Stringer interface {String() string

}

Page 26: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Concurrency (1)

Go - The Cloud Programming Language - An Introduction and Feature Overview26 19.09.2016

Based on the work/paper “Communicating Sequential Processes (CSP)” by Tony

Hoare (1978).

Based on message-passing (CSP, Actor Model), instead of shared memory.

Page 27: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Concurrency (2) - Goroutines

Go - The Cloud Programming Language - An Introduction and Feature Overview27 19.09.2016

A goroutine is a function that is capable of running concurrently with other functions.

Goroutines are lightweight. We can easily create thousands of them.

Use the keyword «go» to start a new goroutine.

package main

import ("fmt""time"

)

func say(s string) {for i := 0; i < 5; i++ {

time.Sleep(100 * time.Millisecond)fmt.Println(s)

}}

func main() {go say("world")say("hello")

}

Page 28: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Concurrency (3) - Channels

Go - The Cloud Programming Language - An Introduction and Feature Overview28 19.09.2016

With channels we can pass safely data between goroutines.

Channels are by default synchronous blocking.

Buffered channels are asynchronous non blocking.

my_channel := make(chan int)

//within some goroutine - to put a value on the channelmy_channel <- 5

//within some other goroutine - to take a value off the channelvar my_recvd_value intmy_recvd_value = <- my_channel

Page 29: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Who Uses Go

Go - The Cloud Programming Language - An Introduction and Feature Overview30 19.09.2016

GopherCon 2016 Sponsors

Page 30: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Go - The Cloud Programming Language - An Introduction and Feature Overview31 19.09.2016

- https://golang.org/

- https://github.com/golang

- …

Page 31: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

List of Sources

Go - The Cloud Programming Language - An Introduction and Feature Overview32 19.09.2016

https://github.com/a8m/go-lang-cheat-sheet

https://talks.golang.org/2015/gophercon-goevolution.slide#1

https://talks.golang.org/2014/hellogophers.slide#1

http://jimplush.com/talk/2015/12/19/moving-a-team-from-scala-to-golang/

http://golangtutorials.blogspot.ch/

https://gobyexample.com/

http://dave.cheney.net/

https://www.golang-book.com/books/intro

http://arild.github.io/csp-presentation/#1

https://en.wikipedia.org/wiki/Communicating_sequential_processes

https://en.wikipedia.org/wiki/Go_(programming_language)

https://www.youtube.com/watch?v=uQgWP7zM6mU

http://www.javaworld.com/article/2929811/scripting-jvm-languages/whats-the-go-language-really-good-for.html

Page 32: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Andrija Sisko

Senior Consultant ZH-AD

Tel. +41 58 459 53 01

[email protected]

19.09.2016 Go - The Cloud Programming Language - An Introduction and Feature Overview33

Page 33: Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko

Session Feedback – now

Go - The Cloud Programming Language - An Introduction and Feature Overview34 19.09.2016

Please use the Trivadis Events mobile app to give feedback on each session

Use "My schedule" if you have registered for a session

Otherwise use "Agenda" and the search function

If the mobile app does not work (or if you have a Windows smartphone), use your

smartphone browser

– URL: http://trivadis.quickmobileplatform.eu/

– User name: <your_loginname> (such as “svv”)

– Password: sent by e-mail...