47

The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Embed Size (px)

Citation preview

Page 1: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ
Page 2: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

David Chisnall

The Go ProgrammingLanguage

P H R A S E B O O K

Upper Saddle River, NJ • Boston • Indianapolis • San FranciscoNew York • Toronto • Montreal • London • Munich • Paris • Madrid

Cape Town • Sydney • Tokyo • Singapore • Mexico City

DEVELOPER’SLIBRARY

Page 3: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Many of the designations used by manufacturers and sellers to distinguish theirproducts are claimed as trademarks. Where those designations appear in this book,and the publisher was aware of a trademark claim, the designations have been print-ed with initial capital letters or in all capitals.The author and publisher have taken care in the preparation of this book, but makeno expressed or implied warranty of any kind and assume no responsibility for errorsor omissions. No liability is assumed for incidental or consequential damages in con-nection with or arising out of the use of the information or programs contained herein.The publisher offers excellent discounts on this book when ordered in quantity forbulk purchases or special sales, which may include electronic versions and/or cus-tom covers and content particular to your business, training goals, marketing focus,and branding interests. For more information, please contact:

U.S. Corporate and Government Sales(800) [email protected]

For sales outside the United States, please contact:International [email protected]

Visit us on the Web: informit.com/awLibrary of Congress Cataloging-in-Publication Data:Chisnall, David.

The Go programming language phrasebook / David Chisnall.p. cm.

Includes index.ISBN 978-0-321-81714-3 (pbk. : alk. paper) — ISBN 0-321-81714-1 (pbk. : alk.

paper)1. Go (Computer program language) 2. Computer programming. 3. Open source

software. I. Title. QA76.73.G63C45 2012005.3—dc23

2012000478Copyright © 2012 Pearson Education, Inc.All rights reserved. Printed in the United States of America. This publication is pro-tected by copyright, and permission must be obtained from the publisher prior to anyprohibited reproduction, storage in a retrieval system, or transmission in any form orby any means, electronic, mechanical, photocopying, recording, or likewise. To obtainpermission to use material from this work, please submit a written request toPearson Education, Inc., Permissions Department, One Lake Street, Upper SaddleRiver, New Jersey 07458, or you may fax your request to (201) 236-3290.ISBN-13: 978- 0-321-81714-3ISBN-10: 0-321-81714-1Text printed in the United States on recycled paper at Edwards Brothers Malloy inAnn Arbor, Michigan.First printing: March 2012

Editor-in-ChiefMark TaubAcquisitions EditorDebra WilliamsCauleyMarketingManagerStephane Nakib

Managing EditorKristy HartProject EditorAnne Goebel

Copy EditorGayle JohnsonPublishingCoordinatorAndrea Bledsoe

Cover DesignerGary AdairSenior CompositorGloria Schurick

Page 4: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Table of Contents

1 Introducing Go 1Go and C 1Why Go? 4Goroutines and Channels 7Selecting a Compiler 10Creating a Simple Go Program 13The Go Type System 14Understanding the Memory Model 16

2 A Go Primer 21The Structure of a Go Source File 23Declaring Variables 26Declaring Functions 29Looping in Go 32Creating Enumerations 35Declaring Structures 37Defining Methods 39Implementing Interfaces 42Casting Types 47

3 Numbers 51Converting Between Strings and Numbers 52Using Large Integers 54Converting Between Numbers and Pointers 56

4 Common Go Patterns 61

Page 5: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

iv Contents

Zero Initialization 62Generic Data Structures 67Specialized Generic Data Structures 69Implementation Hiding 72Type Embedding 75

5 Arrays and Slices 79Creating Arrays 81Slicing Arrays 83Resizing Slices 85Truncating Slices 87Iterating Over Arrays 88

6 Manipulating Strings 91Comparing Strings 92Processing a String One Character at a

Time 94Processing a Partial String 96Splitting and Trimming Strings 98Copying Strings 102Creating Strings from Patterns 102Matching Patterns in Strings 104

7 Working with Collections 107Creating a Map 108Storing Unordered Groups of Objects 111Using Lists 112Defining New Collections 114

Page 6: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Contents v

8 Handling Errors 117Deferring Cleanup 118Panicking and Recovering 121Returning Error Values 125Error Delegates 127

9 Goroutines 131Creating Goroutines 131Synchronizing Goroutines 134Waiting for a Condition 137Performing Thread-Safe Initialization 140Performing Actions in the Background 142Communicating Via Channels 144Using Multiple Channels 148

10 Concurrency Design Patterns 151Timing Out Connections 152Aliased xor Mutable 154Share Memory by Communicating 156Transactions by Sharing Channels 159Concurrent Objects 162Implementing Futures in Go 164Coalescing Events 166Map Reduce, Go Style 168

11 Dates and Times 175Finding the Current Date 176Converting Dates for Display 177

Page 7: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

vi Contents

Parsing Dates from Strings 179Calculating Elapsed Time 180Receiving Timer Events 181

12 Accessing Files and the Environment 183Manipulating Paths 184Reading a File 186Reading One Line at a Time 188Determining if a File or Directory Exists 190Checking Environment Variables 192

13 Network Access 195Connecting to Servers 196Distributing Go 199Serving Objects 204Calling Remote Procedures 206

14 Web Applications 207Integrating with a Web Server 208Connecting to Web Servers 211Parsing HTML 213Generating HTML 216

15 Interacting with the Go Runtime 219Finding the Type of a Variable 220Finalizing Structures 223Copying Arbitrary Types 226Constructing Function Calls 228Calling C Functions 230

Page 8: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Contents vii

16 Distributing Go Code 233Installing Third-Party Packages 234Creating Packages 236Documenting Your Code 240Staying Up to Date 241

17 Debugging Go 243Using a Debugger 243Misunderstanding Memory Ordering 247Spotting Concurrency Bugs 249Restricting Behavior 252Building Unit Tests 257

Index 259

Page 9: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

This page intentionally left blank

Page 10: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

About the AuthorDavid Chisnall is a freelance writer and consultant.While studying for his PhD, he cofounded theÉtoilé project, which aims to produce an open-source desktop environment on top of GNUstep,an open-source implementation of the OpenStepand Cocoa APIs. He is an active contributorto GNUstep and is the original author andmaintainer of the GNUstep Objective-C 2runtime library and the associated compilersupport in the Clang compiler. He is also aFreeBSD committer working various aspects ofthe toolchain, including being responsible for thenew C++ stack.After completing his PhD, David hid in academiafor a while, studying the history of programminglanguages. He finally escaped when he realizedthat there were places off campus with anequally good view of the sea and withoutthe requirement to complete quite so muchpaperwork. He occasionally returns to collaborateon projects involving modeling the semantics ofdynamic languages.When not writing or programming, David enjoysdancing Argentine tango and Cuban salsa,playing badminton and ultimate frisbee, andcooking.

Page 11: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

AcknowledgmentsThe first person I’d like to thank is MarkSummerfield, author of Programming in Go:Creating Applications for the 21st Century. Ifyou finish this book and want to learn more, I’drecommend you pick up a copy. Mark was theperson responsible for making me look at Go inthe first place.The next person I need to thank is YoshikiShibata. Yoshiki has been working on theJapanese translation of this book and, in doingso, has sent me countless emails highlightingareas that could be improved. If you enjoyreading this book then Yoshiki deserves a lot ofthe credit.Finally, I need to thank everyone else who wasinvolved in bringing this book from my texteditor to your hands. A lot of people haveearned some credit along the way. In particular,Debra Williams-Cauley, who masterminded theproject, and Anne Goebel, who shepherded thebook from a draft manuscript to the version younow hold.

Page 12: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

2

A Go Primer

One of the goals of Go was a consistent andunambiguous syntax. This makes it easy fortools to examine Go programs, and also makesit easy to learn. Unhelpful compiler errors makeit difficult to learn a language, as anyone whohas made a typo in C++ code using templateswill know.In C, for example, function and global variabledeclarations have almost the same syntax. Thismeans that the compiler can’t easily tell whichone you meant if you make an error. It gives youhelpful error messages like “expected ;” on a linewhere you don’t think a semicolon is expected atall.The Go grammar was designed to make itpossible for the compiler to tell you moreaccurately what you did wrong. It was alsodesigned to avoid the need to state somethingthat can be easily inferred. For example, if youcreate a variable and set its value to 42, the

Page 13: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

22 CHAPTER 2: A Go Primer

compiler could probably guess that this variableshould be an integer, without it being explicitlystated. If you initialize it with a function call,then the compiler can definitely tell that thetype should be whatever the function returned.This was the same problem that C++ 2011solves with the auto type.Go adopts JavaScript’s idea of semicoloninsertion, and takes it a step further. Any linethat can be interpreted as a complete statementhas a semicolon implicitly inserted at the end bythe parser.1 This means that Go programs canfreely omit semicolons as statement terminators.This adds some constraints, for exampleenforcing a brace style where open braces are atthe end of the line at the start of flow-controlstatements, rather than on their own. If youhappen to be a human, this is unfortunate,because it means that you can’t use the highlyoptimized symmetry recognition paths, whichevolution has spent the last million or so yearsoptimizing in your visual cortex, for recognizingcode blocks.This chapter contains an overview of Go syntax.This is not a complete reference. Some aspectsare covered in later chapters. In particular, allof the concurrency-related aspects of Go arecovered in Chapter 9, Goroutines.

1This is an oversimplification. The exact rules forsemicolon insertion are more complicated, but this rule ofthumb works in most cases.

Page 14: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

The Structure of a Go Source File 23

The Structure of a Go SourceFile

1 package main2 import "fmt"3

4 func main() {5 fmt.Printf("Hello World!\n")6 }

From: hello.go

A Go source file consists of three parts. The firstis a package statement. Go code is arranged inpackages, which fill the rôles of both librariesand header files in C. The package in thisexample is called main, which is special. Everyprogram must contain a main package, whichcontains a main() function, which is the programentry point.The next section specifies the packages that thisfile uses and how they should be imported. Inthis example, we’re importing the fmt package.Once the fmt package has been imported, anyof its exported types, variables, constants, andfunctions can be used, prefixed by the nameof the package. In this simple example, we’recalling Printf(), a function similar to C’sprintf, to print “Hello World!” in the terminal.Although Go uses static compilation, it’simportant to realize that import statementsare much closer to Java or Python import

Page 15: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

24 CHAPTER 2: A Go Primer

directives than to C inclusions. They do notinclude source code in the current compilationunit. Unlike Java and Python packages, Gopackages are imported when the code is linked,rather than when it is run. This ensures that aGo application will not fail because of a missingpackage on the deployment system, at the costof increasing the size of the executable. Packagesin Go are more important than in languages likeJava, because Go only provides access control atthe package level, while Java provides it at theclass level.When you compile a package (from one ormore .go files) with the Gc compiler, you get anobject code file for the package. This includesa metadata section that describes the typesand functions that the package exports. It alsocontains a list of the packages that this packageimports.The input to the 6l linker is always a .6 file forthe main package. This file contains referencesto every package that the main package imports,which may in turn reference further packages.The linker then combines them all.This eliminates one of the most irritatingproblems with building complex C programs:you include a header, and then have to work outwhich library provided it and add the relevantlinker flags. With Go, if a package compiles, itwill link. You don’t have to provide any extraflags to the linker to tell it to link things that

Page 16: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

The Structure of a Go Source File 25

you’ve referenced via import directives.The remainder of a Go file contains declarationsof types, variables, and functions. We’ll explorethat for the rest of this chapter.You may find that you have two packagesthat you want to import that have the samename. This would cause problems in Go. ThebadStyleImport.go example is functionallyequivalent to the example at the start of thissection but renames the fmt package, calling itformat. Renaming a package when you import itis usually a bad idea, because it makes your codeharder for people to read. You should only everuse it when you explicitly need to disambiguatetwo packages with the same name.

0 package main1 import format "fmt"2

3 func main() {4 format.Printf("Hello World!\n")5 }

From: badStyleImport.go

Page 17: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

26 CHAPTER 2: A Go Primer

Declaring Variables

4 var i int5 var Θ float326 var explicitly, typed, pointers *complex1287 int_pointer := &i8 another_int_pointer := new(int)9 generic_channel := make(chan interface{})

From: variables.go

Variables are declared with the var keyword,followed by the variable name, and finallyby the type. The existence of a specifickeyword for variable declarations makes iteasy to differentiate them from other types ofstatements.Writing the type at the end looks weird topeople familiar with C-family languages, but itmakes sense when you read the code. A (typed)variable declaration is an instruction saying, forexample, “declare the variable foo to have thetype int.”One of the variables declared at the start of thissection uses T (theta) as a variable name. Gopermits identifiers to start with any symbols thatUnicode classes as letters. This can sometimesbe very useful, such as if variable names aremathematical quantities. Don’t abuse it, though:the person maintaining your code will not thankyou if you use characters that he can’t type onhis keyboard for frequently used variables.A declaration statement may declare multiple

Page 18: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Declaring Variables 27

variables, but they all have the same type. In C,some may have the type that is written at thestart of the declaration, some may be pointers tothat type, some may be pointers to pointers tothat type, and so on. The form used by Go is farless prone to ambiguity.You will rarely use the long form of declarations.One of the key ideas in writing good code is theprinciple of minimum scope. This means thatthe scope of a variable—the lexical region whereit is valid—should be as small as possible forthe variable’s lifetime. One corollary of this isthat variables should be declared immediatelybefore their first use and initialized as part oftheir declaration.Go provides a shorthand syntax, the :=initialization operator, which does this. Usingthis notation, you can declare and initialize avariable in a single statement. More importantly,you avoid the need to declare a type for thevariable: the type of the variable is the type ofthe expression used to initialize it.The example at the start of this section showsboth kinds of declaration. It also introduces Go’ssyntax for pointers. The variable int_pointeris initialized using the address-of operator (&).This should be familiar to C programmers: itreturns the address in memory of an object.The returned value, however, is more similarto a Java reference than a C pointer. Youcan’t perform arithmetic using Go pointers,

Page 19: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

28 CHAPTER 2: A Go Primer

nor use them interchangeably with arrays. Aswith Java references, you can pass Go pointersaround without having to worry about whenthe underlying object will be deallocated. It willautomatically be freed when the last reference isdestroyed. Unlike Java references, you can makepointers to primitive types, not just to structures(Go’s equivalent of objects).In this example, you could return int_pointerfrom this function without any problems. Thismay seem strange to C programmers, becauseit points to a variable declared locally. The Gocompiler will try to allocate i on the stack,but that’s just an implementation detail. If itsaddress is taken and it is returned from thefunction then it will be allocated on the heapinstead.This example creates another integer pointer,in a different way. The new() built-in functioncreates a new integer and returns a pointer toit. This is semantically equivalent to declaringan integer variable and then taking its address.Neither guarantees how the underlying storagewill be allocated. You can pass any typeto new(), but it is not the standard way ofallocating everything.Go includes three special types, which we’lllook at in a lot more detail later in this book:slices, maps, and channels. These are referencetypes, meaning that you always access them viaa reference. If you assign one map-typed variable

Page 20: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Declaring Functions 29

to another, then you will have two variablesreferring to the same map. In contrast, if youassign one integer-typed variable to another,then you will have two variables with the samevalue, but modifying one will not affect theother.Instances of reference types in Go are createdwith the make() built-in function. This is similarto new(), but also performs initialization of thebuilt-in types. Values returned by new() aresimply zeroed. They are not guaranteed to beimmediately useful, although good style suggeststhat they should be.

Declaring Functions

4 func printf(str string, args ...interface{}) (int, error) {

5 _, err := fmt.Printf(str, args...)6 return len(args), err7 }8

9 func main() {10 count := 111 closure := func(msg string) {12 printf("%d %s\n", count, msg)13 count++14 }15 closure("A Message")16 closure("Another Message")17 }

From: functions.go

Page 21: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

30 CHAPTER 2: A Go Primer

Functions in Go are declared using the funckeyword. As with variable declarations, thereturn type goes at the end. This can be a singlevalue, or a list of values. The printf() functionin the example shows several important featuresof Go. This is a variadic function, which returnsmultiple values: an integer and an error. Theinteger is the number of variadic argumentspassed to it, and the error code is one of thevalues returned from the Printf() function fromthe fmt package.Note the syntax for calling functions that returnmultiple values. The return values must eitherall be ignored, or all assigned to variables. Theblank identifier, _, can be used for values thatyou wish to discard.Variadic functions in Go are particularlyinteresting. In C, a variadic function call justpushes extra parameters onto the stack, andthe callee has to know how to pop them off. InGo, all variadic parameters are delivered as aslice (see Chapter 5, Arrays and Slices; for nowyou can think of a slice as being like an array).The variadic parameters must all be of the sametype, although you can use the empty interfacetype (interface{}) to allow variables of anytype and then use type introspection to find outwhat they really are.The main() function in the example is theprogram entry point. Unlike many otherlanguages, this takes no arguments. Command-

Page 22: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Declaring Functions 31

line arguments and environment variables arestored globally in Go, making it easy to accessthem from any function, not just one near theprogram entry point.Inside this function, you’ll see a closure defined.Closures in Go are declared as anonymousfunctions, inside other functions. The closurecan refer to any variables in the scope whereit is declared. In this example, it refers to thecount variable from the outer function’s scope.It would continue to do so even after the outerfunction returned. In Go, there is no distinctionbetween heap and stack allocated variables,except at the implementation level. If a localvariable is referenced after the function thatcontains it, then it is not freed when the functionreturns. If closure were stored in a globalvariable, for example, then count would not bedeallocated, even after the function returned.

Page 23: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

32 CHAPTER 2: A Go Primer

Looping in Go

1 package main2 import "fmt"3

4 func main() {5 loops := 16 // while loop:7 for loops > 0 {8 fmt.Printf("\nNumber of loops?\n")9 fmt.Scanf("%d", &loops)

10 // for loop11 for i := 0 ; i < loops ; i++ {12 fmt.Printf("%d ", i)13 }14 }15 // Infinite loop16 for {17 // Explicitly terminated18 break19 }20 }

From: loop.go

In C, you have three kinds of loops, all withdifferent syntax and overlapping semantics. Gomanages to have more expressive loop semantics,but simple and uniform syntax.Every loop in Go is a for statement. We’ll onlylook at the forms that mirror C loop constructshere. The form that iterates over a collection isexplained in Chapter 5, Arrays and Slices.The loop.go example shows the three typesof general for loops in Go. The last one isthe simplest. This is an infinite loop, with an

Page 24: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Looping in Go 33

explicit break statement to terminate it. You’dmost commonly use this form for an event loopthat would not terminate in normal use. LikeC, Go also has a continue statement thatimmediately jumps to the start of the next loopiteration, or exits the loop if the loop conditionno longer holds.Both the break and continue statementssupport an optional label for jumping out ofnested loops. Note that the label is not a jumptarget; it is just used to identify the loop.

5 for i := 0 ; i<10 ; i++ {6 L:7 for {8 for {9 break L

10 }11 }12 fmt.Printf("%d\n", i)13 }

From: break.go

You can see this in the break.go example. Thebreak statement jumps out of the two innerloops, but does not prevent the Printf callfrom running. It jumps to the end of the loopimmediately after L:, not to the start.Most of the time, you won’t use infinite loopsand explicit escapes. The other two types offor loops in Go are analogous to while and forloops in C and the older form of for loops in

Page 25: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

34 CHAPTER 2: A Go Primer

Java. The outer loop in the example at the startof this section will test its condition and loop aslong as it is true. The inner loop first performsthe initialization (i := 0) then tests the loopcondition (i < loops), and runs the loop clauseas long as it’s true. Between each loop iteration,it runs the increment clause (i++). If you’ve usedany vaguely C-like language, then this will bevery familiar to you. The only difference betweena Go for loop and a C for loop is that the Goversion does not require brackets.There are a couple of interesting things inthis loop. The first is the creation of the loopvariable (i) at the loop scope. This is similar toC99 or C++. The variable that is declared inthe loop initialization clause is only in scope forthe duration of the loop.The second is the increment statement. Notethat I did not call it a postincrement expression.The designers of Go decided to eliminatethe confusion between preincrement andpostincrement expressions in C. In Go, theincrement statement is not an expression, andonly the suffix syntax is allowed. This lineincrements the variable, but it does not evaluateto anything. Writing something like a := b++is not valid Go. Writing ++b is invalid in allcontexts in Go: there is no prefix form of theoperator.

Page 26: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Creating Enumerations 35

Creating Enumerations

4 const (5 Red = (1<<iota)6 Green = (1<<iota)7 Blue, ColorMask = (1<<iota), (1<<(iota+1))-18 )

From: enum.go

There are several places in Go where it isobvious that someone has spent a lot of thoughtdesigning exactly the right syntax for mostcommon uses of a language feature. Enumerationconstants are the most obvious example of thisattention to detail.There is no divide between constants andenumerations in Go. This mirrors theirimplementation in C, where enumerated typescan be used interchangeably with integers.Groups of constants within the same declarationin Go are used for enumerations.There are two common uses for enumeratedtypes. The first is defining a set of mutually-exclusive options. The second is defining a setof overlapping flags. Typically, you’ll use asequence of numbers for the first and a sequenceof powers of 2 for the second. You can thencreate a bitfield by bitwise-oring a combinationof enumeration values together.In C, and most other languages with enumeratedtypes, you need to explicitly provide the

Page 27: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

36 CHAPTER 2: A Go Primer

numerical values for the second type ofenumeration. The first will be automaticallynumbered by the compiler.Go provides a much more flexible mechanismfor defining enumerations. The iota predeclaredidentifier is similar to the GNU C __COUNTER__preprocessor macro, but it’s more powerful. Itis an integer constant expression. In a normalprogram scope, it evaluates to zero, but in thescope of a constant declaration it is initially zerobut then incremented on each line where it isused.Unlike __COUNTER__, iota is scoped. It is zeroon the first line of the group in this example, andwill always be zero in the first line of this group,irrespective of how it is used elsewhere. If youhave multiple const groups in a single sourcefile, then iota will be zero at the start of each ofthem.The example at the start of this section showshow to declare a group of constants for use asan enumerated type. This simple example showsthe low 3 bits of a bitfield being used to storethree flags indicating the presence of three colorvalues. The ColorMask constant is defined toprovide the value that must be bitwise-and’dwith an integer to give the three color flags.It’s possible to reference constants from otherconstant declarations, so you can combine thiskind of declaration easily. For example, youcould provide another constant declaration

Page 28: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Declaring Structures 37

describing another group of flags within a set,and then extend this declaration to use them inthe next few bits of the bitfield.Similarly, you can extend existing constantdeclarations by inserting another iota expressionearlier. This will then renumber all subsequentvalues, so it’s important to be careful when theconstants are part of a binary interface.Constants—and therefore enumerations—inGo are not limited to integers. Other typescan be specified in the same way. The enum.goexample also shows the declaration of thecomplex constant i, with the same definition asin mathematics.

10 const (11 i complex128 = complex(0, 1)12 )

From: enum.go

Declaring Structures

4 type Example struct {5 Val string6 count int7 }

From: struct.go

Structures in Go are somewhat richer than

Page 29: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

38 CHAPTER 2: A Go Primer

C structures. One of the most importantdifferences is that Go structures automaticallysupport data hiding.Any top-level type, method, or variable namethat starts with a capital letter is visible outsideof the package in which it is declared. Thisextends to structure fields. In C, if you only putsome fields from a structure in a header, thenyou will encounter problems when someone triesto allocate an instance of it on the stack: hiscompiler won’t allocate enough space for it. Gopackages export the offsets of the public fields.This allows them to be created and their publicfields accessed from other compilation units.The example at the start of this section definesa structure with two fields. The first, a string, ispublic and can be accessed from anywhere. Thesecond, an integer, is private and is only visibleto code in the same package as this definition.A structure doesn’t have to declare any publicfields. You can create opaque types by defining astructure where all of the fields are private.If you’re coming from a class-based languagelike C++ or Java, then you may be wonderingwhy there are public and private fields, butnot protected ones. The answer is quite simple:there is no inheritance in Go, so protected wouldhave no meaning. Public and private also haveslightly different meanings in Go and a languagelike Java. A private field in a Go structure canbe accessed by any code in the same package,

Page 30: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Defining Methods 39

not just by methods of that structure. If youcome from Objective-C, then you can think ofprivate fields in Go structures like @packageinstance variables in Objective-C. If you comefrom C++, then think of all Go functions ina package as implicitly being friends of allstructures declared in the same package.

Defining Methods

9 type integer int10 func (i integer) log() {11 fmt.Printf("%d\n", i);12 }13 func (e *Example) Log() {14 e.count++15 fmt.Printf("%d %s\n", e.count, e.Val)16 }

From: methods.go

If you’ve used a class-based language, then youare probably wondering why the last exampledidn’t define any methods defined on thestructure. In Go, you may define methods onany concrete type that you define, not just onstructures. The example at the start of thissection defines two Log() methods—recall thatthe uppercase start letter makes them public—one on the structure defined in the last sectionand one on a named integer type.The Go type system lets you assign any int tothis named type without an explicit cast, but not

Page 31: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

40 CHAPTER 2: A Go Primer

vice versa. It also prevents you from assigningbetween two named types. This can be veryuseful for variables representing quantities. Youcould, for example, define kilometer and miletypes and have the compiler reject any codewhere you attempted to assign one to the other.You cannot add methods to existing types—Go does not have an equivalent of Objective-Ccategories—but you can define new named typesand add methods to them.Methods are declared just like functions,except that there is one extra parameter—thereceiver—declared before the function name.One of the interesting syntactic quirks of Gois that there is no this or self keyword. Youcan give the receiver any name that you want,and this name does not have to be consistentbetween methods. This idea comes from Oberon-2 and should be popular with people who likethe “no magic” philosophy of languages likeObjective-C: the receiver is not an implicithidden parameter that the compiler inserts; itis an explicit parameter just like any other.The method on the structure in the exampleat the start of this section takes a pointer asthe receiver. This means that it can modifyfields of the receiver and these changes will beshared. Methods do not have to take pointers:the other method in the example takes a value.If a method takes a value type, then it can stillbe called with either a value or a pointer, but it

Page 32: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Defining Methods 41

will receive a copy of the structure, so changesthat it makes will not be visible from the caller.When talking about expressions with an explicittype, methods are just functions. You call amethod on a structure by using the dot notation,and you declare the parameter that declareshow the structure is passed to the method in aspecial way, but this is just some syntactic sugar.Methods called in this way are semanticallyequivalent to functions that just take the receiveras an argument: they are statically resolved andare just function calls.That’s not the real power of methods, though.When you call a method via an interface(described in the next section), you get late-bound dynamic lookup. This dual nature ofGo methods means that you have a singleabstraction that can be used in the same wayas either C types or Smalltalk objects. If yourequire performance, then you can use staticallytyped definitions and avoid the dynamic lookup.If you require flexibility, then you can use thelate binding mechanism of interfaces.

Page 33: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

42 CHAPTER 2: A Go Primer

Implementing Interfaces

5 type cartesianPoint struct {6 x, y float647 }8 type polarPoint struct {9 r, θ float64

10 }11

12 func (p cartesianPoint) X() float64 {return p.x }13 func (p cartesianPoint) Y() float64 {return p.y }14 func (p polarPoint) X() float64 {15 return p.r*math.Cos(p.θ)16 }17 func (p polarPoint) Y() float64 {18 return p.r*math.Sin(p.θ)19 }20 func (self cartesianPoint) Print() {21 fmt.Printf("(%f, %f)\n", self.x, self.y)22 }23 func (self polarPoint) Print() {24 fmt.Printf("(%f, %f◦)\n", self.r, self.θ)25 }26 type Point interface {27 Printer28 X() float6429 Y() float6430 }31 type Printer interface {32 Print()33 }

From: interface.go

The dynamic dispatch mechanism in Go isreminiscent of StrongTalk, a strongly typedSmalltalk dialect. Interfaces describe a set ofmethods that a type understands. Unlike Java

Page 34: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Implementing Interfaces 43

interfaces or Objective-C protocols, they do notneed to be explicitly adopted.Any type can be assigned to a variable withan interface type, as long as it implements allof the required methods. In some cases, thiscan be checked at compile time. For example,if one interface is a superset of another, thencasting from the superset to the subset is alwaysvalid, as is casting from a structure type toan interface when the compiler sees that thestructure implements the interface.In other cases, it is not. These cases requirea type assertion, detailed in the next section,which will generate a runtime panic if they fail.This means that any variable with an interfacetype is guaranteed to either be nil, or hold avalid value of a concrete type that implementsthe interface.The example from the start of this sectionshows the creation of new structure types,and interfaces that they implement. Notethat the structure can be defined before theinterface. In fact, structures can be defined inentirely different packages to interfaces. This isespecially useful if various third-party structuresall implement the same method or group ofmethods: you can define a new interface thatcan be any one of them.There are two interfaces declared in thisexample, both following Go naming conventions.The Printer interface defines a single method,

Page 35: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

44 CHAPTER 2: A Go Primer

Note: If you’re coming from C++, then youshould use interfaces in most of the places whereyou’d use templates in C++. Rather than definingtemplate functions or classes (which Go doesn’tsupport), define an interface that specifies the setof methods that you need, and use it where youwould use the template parameter in C++.

so it follows the convention of appending the -ersuffix to the method name to give the interfacename.The other interface uses interface compositionto extend the Printer interface. This onedefines an abstract data type. It providesmethods for accessing the horizontal and verticalcoordinates of a two-dimensional point. Interfacecomposition is effectively equivalent to interfaceinheritance in Java. You can use it in someplaces where you would consider using single ormultiple inheritance in other languages.This example provides two structures thatimplement this interface, one using Cartesianand the other using polar coordinates. This is asimple example of how an interface can be usedto hide the implementation. The two structuresboth start with lowercase letters, so they willnot be exported from this package, while theinterfaces will. You could extend this exampleby providing functions to construct a Point frompolar and Cartesian coordinates, each returning

Page 36: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Implementing Interfaces 45

one of a different kind of structure.When you are dealing with interfaces, thedistinction between methods that take pointersand ones that take values becomes moreimportant. If you tried to assign an instanceof this example structure to an interface thatrequired the Log() method, then the assignmentwould be rejected. Assigning a pointer to aninstance of this structure would work.This seems counterintuitive. If you have avalue, then you can always take its address toget a pointer, so why are the two method setsdistinct? The answer is very simple: it helpsavoid bugs. When you pass a value, you createa copy of a structure. When you pass a pointer,you alias the structure. If you pass a valueand then implicitly, via method invocation onan interface, pass a pointer, then any changesthat the method made would be made to thetemporary copy, not to the original structure.This is probably not what you want, and if itis then you can just pass the pointer originally,rather than the copy.The Go FAQ gives an example of a case wherethis could be problematic:var buf bytes.Bufferio.Copy(buf, os.Stdin)

The io.Copy() function copies data fromsomething that implements the io.Readerinterface to something that implements theio.Writer interface. When you call this

Page 37: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

46 CHAPTER 2: A Go Primer

function, it will pass a copy of buf as the firstargument, because Go always passes by value,not by reference. It will then try to copy datafrom the standard input into the new copy ofbuf. When the function returns, the copy ofbuf will no longer be referenced, so the garbagecollector will free it.What the person writing this code probablywanted to do was copy data from the standardinput into buf. The Go type system willreject this, because buf does not implementthe io.Writer interface: the method forwriting bytes to a buffer modifies the bufferand therefore requires a pointer receiver. Bydisallowing this, Go lets you get an error atcompile time and trivially fix it by writing thisinstead:

var buf bytes.Bufferio.Copy(&buf, os.Stdin)

If Go allowed values to use methods that aredeclared as requiring a pointer, then you wouldinstead spend ages wondering why this lineappeared to be reading the correct amountof data, but wasn’t storing any of it in thebuffer that you declared. This is part of theGo philosophy of avoiding ambiguity. It justtakes one extra character to make a pointerwhen you need one. That small amount of extraeffort is a lot less than the time you’d spenddebugging code where you meant one thing andGo assumed that you meant something else.

Page 38: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Casting Types 47

Casting Types

4 type empty interface {}5 type example interface {6 notImplemented()7 }8

9 func main() {10 one := 111 var i empty = one12 var float float3213 float = float32(one)14 switch i.(type) {15 default:16 fmt.Printf("Type error!\n")17 case int:18 fmt.Printf("%d\n", i)19 }20 fmt.Printf("%f\n", float)21 // This will panic at run time22 var e example = i.(example)23 fmt.Printf("%d\n", e.(empty).(int))24 }

From: cast.go

Unlike C, Go does not allow implicit casting.This is not laziness on the part of theimplementors: implicit casting makes it easyfor very subtle bugs to slip into code. I recentlyhad to find a bug in some code that hadgone undetected for several years, where animplicit cast meant that a value was incorrectlyinitialized. The code looked correct, until youchecked the type declarations of everythinginvolved, which were spread over multiple files.

Page 39: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

48 CHAPTER 2: A Go Primer

This is another example of the Go philosophy.You should never need to state the obviousto the compiler, but you should always haveto explicitly specify things that are otherwiseambiguous.The example at the start of this section showsseveral casts. The concept of casting in otherlanguages is embodied by two concepts in Go.The first is type conversion; the second is typeassertion.A type conversion is similar to a cast in C.It reinterprets the value as a new type. Theconversion from int to float32 is an example ofthis. The resulting value is a new floating-pointvalue with the same value as the integer. Insome cases, the conversion is only approximate.For example, a conversion in the other directionwill result in truncation. A type conversion froman integer to a string type will return a single-character string interpreting the integer as aunicode value.Type assertions are more interesting. They donot convert between types; they simply state tothe compiler that the underlying value has thespecified type. This assertion is checked at runtime. If you try running this example, you willsee that it aborts with a runtime panic.This is because of the type assertion tellingthe compiler that the type of i is somethingthat implements the example interface. Infact, the underlying type is int, which does

Page 40: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Casting Types 49

1 12 1.0000003 panic: interface conversion: int is not main.

example: missing method notImplemented4

5 goroutine 1 [running]:6 main.main()7 /Users/theraven/Documents/Books/GoPhrasebook/

startsnippets/cast.go:22 +0x20d8

9 goroutine 2 [syscall]:10 created by runtime.main11 /Users/theraven/go/src/pkg/runtime/proc.c:21912 exit status 2

Output from: cast.go

not implement the notImplemented() methodthat this interface specifies. The type check failson the type assertion. If you come from C++,you can think of type assertions as roughlyequivalent to a dynamic_cast that throws anexception2 on failure.The final cast-like construct in Go is the typeswitch statement. This is written like a normalswitch statement, but the switch expression isa type assertion to type and the cases have typenames, rather than values.The type switch in the example is used as asimple type check, like a C++ dynamic_cast.

2Runtime panics are not quite analogous to C++exceptions. The differences are covered in Chapter 8,Handling Errors.

Page 41: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

50 CHAPTER 2: A Go Primer

It is more common to use type switches whendefining generic data structures (see Chapter 4,Common Go Patterns) to allow special cases forvarious types.

Page 42: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Index

Symbols

&, see address-ofoperator

6g, 10

A

address-ofoperator, 27

associative arrays,107

atomic package,209

B

big package, 228binary semaphores,

136blank identifier, 30break statement,

32bufio package, 188

C

C pseudo-package,230

cgo tool, 230channels, 28, 144Communicating

SequentialProcesses, 8

composedcharacters, 96

condition variables,137

continuestatement, 32

CSP, seeCommunicatingSequentialProcesses, 144

D

data hiding, 38deadlock, 251

Page 43: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

260 Index

defensiveprogramming,224

defer statement,119

dictionaries, 107duck typing, 15

E

empty interface, 15empty interface

type, 30, 67, 113enumerated types,

35environment

variables, 192error delegate

pattern, 127errors package, 126exp/html package,

213exp/utf8string

package, 100

F

fastcgi package,209

filepath package,184

fmt package, 30,52, 165, 209

func keyword, 29futures, 164

G

garbage collection,16

GC, see garbagecollection

GCC, see GNUCompilerCollection

GDB, see GNUDebugger

GNU CompilerCollection, 11

GNU Debugger,244

gobuild, 238doc, 240fix, 241install, 234,

238test, 257

Page 44: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Index 261

go command, 14go package, 219gob, 205godoc, 240gomake, 231goroutine, 7goroutines

creating, 131synchronizing,

134

H

hash tables, 107html/template

package, 218

I

incrementstatement, 34

inheritance, 75interface

composition, 44introspection, 15iota, 36

J

jsonrpc package,204

L

lazy initialization,141

linked lists, 112list package, 112lists, 112

M

make() built-infunction, 29, 109

Map Reduce, 168maps, 28, 107math/big package,

54multibyte character

encodings, 95multiple return

values, 126mutex, 134

Page 45: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

262 Index

N

namingconventions, 43

net/http package,208

net/rpc package,204

netchan package,200

new() built-infunction, 28

O

Ogle, 243opaque types, 38os package, 186,

224

P

packageatomic, 209big, 228bufio, 188C, 230errors, 126exp/html, 213

exp/utf8string,100

fastcgi, 209filepath, 184fmt, 30, 52,

165, 209go, 219html/template,

218jsonrpc, 204list, 112math/big, 54net/http, 208net/rpc, 204netchan, 200os, 186, 224reflect, 217,

220regexp, 104runtime, 176strconv, 53strings, 99sync, 134, 142,

173text/template,

216textproto, 207time, 175, 176unicode, 99unicode/utf8,

96unsafe, 57

Page 46: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

Index 263

xml, 215packages, 234panic, 81postincrement

expression, 34principle of

minimum scope,27

producer-consumerproblem, 138

promises, 165

R

RAII, see resourceacquisition isinitialization

receive statement,146

receiver, 40reference types, 28reflect package,

217, 220regexp package,

104regular expressions,

104

Remote ProcedureCall, 204

resource acquisitionis initialization,62

RPC, see RemoteProcedure Call

runtime package,176

S

segmented stacks,9

select statement,148

semicolon insertion,22

send statement,146

slice, 28, 30, 83strconv package,

53strings package, 99subclassing, 75sync package, 134,

142, 157, 173

Page 47: The Go Programming Language Phrasebookptgmedia.pearsoncmg.com/images/9780321817143/samplepages/... · David Chisnall The Go Programming Language PHRASEBOOK Upper Saddle River, NJ

264 Index

T

text/templatepackage, 216

textproto package,207

time intervals, 175time package, 175,

176two-stage creation

pattern, 63type assertion, 43,

48, 254type conversion, 48type inference, 16type switch

statement, 49,123

U

unicode package,99

unicode/utf8package, 97

unsafe package, 57

V

variadic function,30, 185

W

wait groups, 142

X

xml package, 215

Z

zero initializationpattern, 62, 73,134

zero value, 62, 111