Introduction to Go programming
Transcript of Introduction to Go programming
![Page 2: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/2.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/3.jpg)
Challenges in Modern Compute Landscape
![Page 4: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/4.jpg)
EnvironmentMulti-CoreNetworked SystemsComputational ClustersWeb Programming ModelThousands of ProgrammersSame code baseScale of Development
![Page 5: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/5.jpg)
Legacy LanguagesLanguage Feature ComplexGarbage CollectionMissing language support for ConcurrencyCompilation TimesDependency Management
![Page 6: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/6.jpg)
Programmers digress from the real task!
Crux:
![Page 7: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/7.jpg)
Focus
![Page 8: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/8.jpg)
“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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/9.jpg)
History
![Page 10: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/10.jpg)
HistoryInception: 2007Go 1.0 released in 2012Rob Pike, Ken Thompson and Robert Griesemer
![Page 11: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/11.jpg)
HistoryBinary = 2000 X C++ Source BytesUnused Library IncludesConditional IncludesExtremely Slow Compilation
Even on Distributed Compilation System!
![Page 12: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/12.jpg)
Desired FeaturesLight WeightIncrease Programmer EfficiencyCompiled LanguageConcurrencyGarbage CollectionStatically Typed
![Page 13: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/13.jpg)
How is it Achieved?
![Page 14: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/14.jpg)
HowNo unused import packagesDependency graph is preciseNo dependency cyclesOnly exported data availableOne object both exported and complete data
![Page 15: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/15.jpg)
How40X faster compilation times than C++!
![Page 16: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/16.jpg)
Differences between Go and C
![Page 17: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/17.jpg)
SyntaxImport paths are URLsCleaner syntax
![Page 18: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/18.jpg)
Syntax//Ctypedef struct A { int a; char b; }
//Gotype A struct { a int b char }
![Page 19: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/22.jpg)
ScopesUniverse (language identifiers)PackageFileFunctionBlock
![Page 23: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/24.jpg)
ConcurrencyGo RoutinesChannels - Communication Pipes
![Page 25: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/27.jpg)
Concurrency
Shared pipes connecting concurrent go routinesChannels
![Page 28: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/29.jpg)
GCEasier for ProgrammerNo overhead of memory allocation and freeingNo explicit memory freeing
![Page 30: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/30.jpg)
InterfacesSet of methodsMethods on any typeData types implementing all methods satisfy
![Page 31: Introduction to Go programming](https://reader035.fdocuments.in/reader035/viewer/2022062223/586e730c1a28ab99598b5303/html5/thumbnails/31.jpg)
Thank You!SMITA [email protected]