introduction to go programming

31
SMITA VIJAYAKUMAR Go Programming INTRODUCTION TO [email protected]

Upload: exotel

Post on 16-Apr-2017

8.579 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Introduction to Go programming

SMITA VIJAYAKUMAR

Go ProgrammingINTRODUCTION TO

[email protected]

Page 2: Introduction to Go programming

AgendaModern Compute Environment

Focus: What Problem does it Solve?

History

How is it Achieved

For C Programmers - Major Differences

Page 3: Introduction to Go programming

Challenges in Modern Compute Landscape

Page 4: Introduction to Go programming

EnvironmentMulti-CoreNetworked SystemsComputational ClustersWeb Programming ModelThousands of ProgrammersSame code baseScale of Development

Page 5: Introduction to Go programming

Legacy LanguagesLanguage Feature ComplexGarbage CollectionMissing language support for ConcurrencyCompilation TimesDependency Management

Page 6: Introduction to Go programming

Programmers digress from the real task!

Crux:

Page 7: Introduction to Go programming

Focus

Page 8: Introduction to Go programming

“Go’s purpose is therefore not to do research into programming language design; it is to improve the working

environment for its designers and their coworkers.”–Rob Pike

(Co-Designer of Go and Distinguished Engineer at Google)

Page 9: Introduction to Go programming

History

Page 10: Introduction to Go programming

HistoryInception: 2007Go 1.0 released in 2012Rob Pike, Ken Thompson and Robert Griesemer

Page 11: Introduction to Go programming

HistoryBinary = 2000 X C++ Source BytesUnused Library IncludesConditional IncludesExtremely Slow Compilation

Even on Distributed Compilation System!

Page 12: Introduction to Go programming

Desired FeaturesLight WeightIncrease Programmer EfficiencyCompiled LanguageConcurrencyGarbage CollectionStatically Typed

Page 13: Introduction to Go programming

How is it Achieved?

Page 14: Introduction to Go programming

HowNo unused import packagesDependency graph is preciseNo dependency cyclesOnly exported data availableOne object both exported and complete data

Page 15: Introduction to Go programming

How40X faster compilation times than C++!

Page 16: Introduction to Go programming

Differences between Go and C

Page 17: Introduction to Go programming

SyntaxImport paths are URLsCleaner syntax

Page 18: Introduction to Go programming

Syntax//Ctypedef struct A { int a; char b; }

//Gotype A struct { a int b char }

Page 19: Introduction to Go programming

Syntax/* C *///Expression Syntaxint main(int argc, char *argv[])

// The Clockwise-Spiral Rule in C syntax parsingint (*(*fp)(int (*)(int, int), int))(int, int)

/* Go */// Type Syntaxfunc main(argc int, argv []string) int

//Left to rightf func(func(int,int) int, int) func(int, int) int

Page 20: Introduction to Go programming

Syntax//Go can also return multiple value from function func ReadFile(r io.Reader) (num int, err error) { ...}...n, err := ReadFile(r)

Page 21: Introduction to Go programming

Syntaxpackage util//Counter is visible when package is imported var Counter int

//name is not var name string

//Seenfunc ThisIsAlsoAvailable() error { }

//not seenfunc thisIsAPrivateFunction() error { }

Page 22: Introduction to Go programming

ScopesUniverse (language identifiers)PackageFileFunctionBlock

Page 23: Introduction to Go programming

SemanticsNo pointer arithmeticsNo implicit numeric conversionsArray bounds are always checkedtypes are not type aliases

type X int // X and int are distinct

Interface typesReflectionType switch

Page 24: Introduction to Go programming

ConcurrencyGo RoutinesChannels - Communication Pipes

Page 25: Introduction to Go programming

Concurrency

Light-weight threads of executionManaged by Go RuntimeRun concurrently with other go routinesExample

Shared resource manager QuerierDB Readeretc...

Go Routines

Page 26: Introduction to Go programming

Concurrencypackage main import “fmt”

func myPrinter(str string) { fmt.Println(str)}

func main() { myPrinter(“one”) go myPrinter(“maybetwo”) go myPrinter(“maybethree”)}

//Output one maybetwo Maybethree

//Orone maybethree maybetwo

//Orone...

Page 27: Introduction to Go programming

Concurrency

Shared pipes connecting concurrent go routinesChannels

Page 28: Introduction to Go programming

Concurrencypackage main

import "fmt”

func main() { messages := make(chan string)

// Send a value `channel <-` go func() { messages <- "ping" }() //continued

//continued // Receive a value `<-channel` msg := <-messages fmt.Println(msg)}

//Output ping

Page 29: Introduction to Go programming

GCEasier for ProgrammerNo overhead of memory allocation and freeingNo explicit memory freeing

Page 30: Introduction to Go programming

InterfacesSet of methodsMethods on any typeData types implementing all methods satisfy

Page 31: Introduction to Go programming

Thank You!SMITA [email protected]