Transcription of The Programming Language Go - McMaster University
1 The Programming Language GoThe Programming Language GoJessica PavlinDepartment of Computing and SoftwareMcMaster UniversityNovember 16, 2010 The Programming Language GoOutline1 Who & Why2 HowBasic StructureTypes and InterfacesConcurrencyImplementation3 Concluding RemarksThe Programming Language GoWho & WhyWho Designed and Implemented Go?The Programming Language GoWho & WhyA Very Brief HistorySept. 2007 Robert Griesemer, Rob Pike and Ken Thompsonstarted sketching the goals for a new Language ona white boardSept. 2007 Within a few days they had their goals and plansketched outSept. 2007 They continued to design the new languagewhenever they had timeJan. 2008 Thompson started the compilerMay 2008 Taylor started the gcc front end for Go using specsLate 2008 Russ Cox joined in helped to implement thelanguage and librariesThe Programming Language GoWho & WhyA Very Brief HistorySept.
2 2007 Robert Griesemer, Rob Pike and Ken Thompsonstarted sketching the goals for a new Language ona white boardSept. 2007 Within a few days they had their goals and plansketched outSept. 2007 They continued to design the new languagewhenever they had timeJan. 2008 Thompson started the compilerMay 2008 Taylor started the gcc front end for Go using specsLate 2008 Russ Cox joined in helped to implement thelanguage and librariesThe Programming Language GoWho & WhyMotivation for a New LanguageFrustration with existing languages and environments forsystems programmingThey felt that Programming languages were partly to blamefor Programming becoming too difficult Didn t want to have to choose anymore between:Efficient compilationEfficient executionEase of programmingThe Programming Language GoWho & WhyGoals for a New Language1As easy to program as an interpreted, dynamically typedlanguage2 The efficiency and safety of a statically typed, compiledlanguage3 Modern.
3 Support for networked and multicore computing4 Fast5 Make Programming fun againThe Programming Language GoWho & WhyA Very Brief HistorySept. 2007 September 2007: Robert Griesemer, Rob Pikeand Ken Thompson started sketching the goals fora new Language on the white boardSept. 2007 Within a few days they had their goals and plansketched outSept. 2007 They continued to design the new languagewhenever they had timeJan. 2008 Thompson started the compilerMay 2008 Taylor started the gcc front end for Go using specsLate 2008 Russ Cox joined in helped to implement thelanguage and librariesThe Programming Language GoWho & WhyA Very Brief HistorySept. 2007 September 2007: Robert Griesemer, Rob Pikeand Ken Thompson started sketching the goals fora new Language on the white boardSept.
4 2007 Within a few days they had their goals and plansketched outSept. 2007 They continued to design the new languagewhenever they had timeJan. 2008 Thompson started the compilerMay 2008 Taylor started the gcc front end for Go using specsLate 2008 Russ Cox joined in helped to implement thelanguage and librariesThe Programming Language GoWho & WhyA Very Brief HistorySept. 2007 September 2007: Robert Griesemer, Rob Pikeand Ken Thompson started sketching the goals fora new Language on the white boardSept. 2007 Within a few days they had their goals and plansketched outSept. 2007 They continued to design the new languagewhenever they had timeJan. 2008 Thompson started the compilerMay 2008 Taylor started the gcc front end for Go using specsLate 2008 Russ Cox joined in helped to implement thelanguage and librariesThe Programming Language GoWho & WhyA Very Brief HistorySept.
5 2007 September 2007: Robert Griesemer, Rob Pikeand Ken Thompson started sketching the goals fora new Language on the white boardSept. 2007 Within a few days they had their goals and plansketched outSept. 2007 They continued to design the new languagewhenever they had timeJan. 2008 Thompson started the compilerMay 2008 Taylor started the gcc front end for Go using specsLate 2008 Russ Cox joined in helped to implement thelanguage and librariesThe Programming Language GoHowGo s AncestorsBasic Syntax:CPascalConcurrency:NewsqueakLimbo The Programming Language GoHowGo s AncestorsBasic Syntax:CPascalConcurrency:NewsqueakLimbo The Programming Language GoHowBasic StructureHello Worldpackage mainimport fmt "fmt"func main() { ("Hello, world")Hello World!}
6 In GoBasic structure of a Go programThe Programming Language GoHowBasic StructureHello Worldpackage mainimport fmt "fmt"func main() { ("Hello, world")Hello World! in GoAll source code requires a package nameThe Programming Language GoHowBasic StructureHello Worldpackage mainimport newName "fmt"func main() { ("Hello, world")Hello World! in GoYou can import packagesImported packages can have qualified identifiersThe Programming Language GoHowTypes and InterfacesObjects in GoGo is Object-Oriented (OO)-ishHas types and methodsAllows for OO programmingAll types can have methods (even integers and strings) Objects implicitly satisfy interfacesNot an OO Language :No classesNo subclassingThe Programming Language GoHowTypes and InterfacesTypesDefinition:A type determines the set of values and operations specificto values of that typeSyntax:Type = TypeName | TypeLit |"(" Type ")".}}
7 TypeName = = ArrayType | StructType |PointerType | FunctionType |InterfaceType | SliceType |MapType | ChannelType .The Programming Language GoHowTypes and InterfacesAnonymous TypesTypes can be anonymous:type ABCstruct{xfloatintstring}c := ABC{ , 7, "hello" } ( , , )An example of an anonymous 7 helloThe Programming Language GoHowTypes and InterfacesTypes Exampletype Dayintvar dayName = []string{"Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday","Saturday"}Using integer types as days of the Programming Language GoHowTypes and InterfacesInitializing Types: var vs. :=Different ways to initialize types:1varInitializes azeroedinstance of the typeInitializes int, float, etc., and any new type Tvar v1 ABC // 1. type ABC2:=Initializes a to a value if providedCompiler guesses type if not pvar i intj := 0 // i == j = true!
8 Var k intk = 3l := 3 // k == l = true!The Programming Language GoHowTypes and InterfacesInitializing Types: new vs. makeDifferent ways to initialize types:1newReturns areferenceto a newly allocated, zeroed instanceof type TCreates new instances of types not listed in 3v2 := new(ABC) // 2. type*ABC2makeReturns an initialized (not zero) value of type T (not T*)Creates slices, maps and channels onlyvar v3 []int = make([]int, 100)Slice to reference to new array of 100 intsThe Programming Language GoHowTypes and InterfacesArraysArrays are different then they are in languages like C:Arrays are valuesAssigning an array to another copies all of the elementsWhen passing an array to a function, the function receivesa copy of elementsThe size of an array is part of it s type[3]int{ 1, 2, 3 }[10]int{ 1, 2, 3 }[.]
9 ]int{ 1, 2, 3 }[10]int{ 2:1, 3:1, 5:1, 7:1 }The Programming Language GoHowTypes and InterfacesSlices DefinedDefinition:A reference to a contiguous segment of an array andcontains a numbered sequence of elements from that arraySyntax:SliceType = "[" "]" ElementType .The Programming Language GoHowTypes and InterfacesUnderstanding SlicesDetails:Slices wrap arrays to give a more flexible, powerful, andconvenient interface to sequences of dataConceptually, slices have 3 elements: base arrayreference, length, capacityRun-time data still passed by value (pointer, length andcapacity (max length))Length of a slice may change (so long as it still fits withinthe limits of the underlying array)var a []inta = ar[7:9];var slice = []int{ 1,2,3,4,5 }Sample initializations of slicesThe Programming Language GoHowTypes and InterfacesCreating Methods for TypesSyntax:MethodExpr = ReceiverType ".
10 " MethodName .ReceiverType = TypeName |"(" "*" TypeName ")" .Sample method for our previously defined typefunc (d Day) String() string {if0 <= d &&int(d) < len(dayName) {returndayName[d] }return"NoSuchDay"}Using integer types as days of the Programming Language GoHowTypes and InterfacesInterfacesDefinition:An interface type specifies a method set called its interfaceSyntax:InterfaceType = "interface""{" { MethodSpec ";" } "}" .MethodSpec = MethodName Signature |InterfaceTypeName .The Programming Language GoHowTypes and InterfacesInterface Exampletype Stringer interface {String() string}func print(args ..Stringer) {fori, s := range args {ifi > 0 { (" ") } ( ())}}Example of an interface for the Stringer functionprint(Day(1))=> MondayThe Programming Language GoHowTypes and InterfacesExample of a General Interfacefunc print(args.)