ebook img

Programming in Go. Creating Applications for the 21st Century PDF

480 Pages·2012·4.662 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming in Go. Creating Applications for the 21st Century

Programming in Go Creating Applicationsfor the 21st Century Mark Summerfield VisitusontheWeb:informit.com/aw Libraryof CongressCataloging-in-PublicationData Summerfield,Mark. ProgramminginGo:creatingapplicationsforthe21stcentury/MarkSummerfield. p.mcm. Includesbibliographicalreferencesandindex. ISBN978-0-321-77463-7 (pbk.:alk. paper) 1. Go(Computerprogramlanguage) 2. Computerprogramming 3. Applicationsoftware— Development I.Title. QA76.73.G63S86 2012 005.13’3—dc23 2012001914 Copyright©2012QtracLtd. ISBN-13: 978-0-321-77463-7 ISBN-10: 0-321-77463-9 TextprintedintheUnitedStatesonrecycledpaperatRRDonnelleyinCrawfordsville,Indiana. Firstprinting,April2012 Contents at a Glance Tables .............................................................. xv Introduction ...................................................... 1 Chapter1. AnOverviewinFiveExamples ...................... 7 Chapter2. BooleansandNumbers................................ 51 Chapter3. Strings ................................................ 81 Chapter4. CollectionTypes ...................................... 139 Chapter5. ProceduralProgramming ............................ 185 Chapter6. Object-OrientedProgramming ...................... 253 Chapter7. ConcurrentProgramming ............................ 315 Chapter8. FileHandling .......................................... 361 Chapter9. Packages .............................................. 407 AppendixA. Epilogue ............................................ 435 AppendixB. TheDangersof SoftwarePatents .................. 437 AppendixC. SelectedBibliography .............................. 441 Index .............................................................. 443 www.qtrac.eu/gobook.html Contents Tables .............................................................. xv Introduction ...................................................... 1 WhyGo? ........................................................ 1 TheStructureof theBook ........................................ 4 Acknowledgments ................................................ 5 Chapter1. AnOverviewinFiveExamples ........................ 7 1.1. GettingGoing ................................................ 7 1.2. Editing,Compiling,andRunning ............................ 9 1.3. HelloWho? .................................................. 14 1.4. BigDigits—Two-DimensionalSlices .......................... 16 1.5. Stack—CustomTypeswithMethods .......................... 21 1.6. Americanise—Files,Maps,andClosures ...................... 29 1.7. PolartoCartesian—Concurrency ............................ 40 1.8. Exercise .................................................... 48 Chapter2. BooleansandNumbers ................................ 51 2.1. Preliminaries ................................................ 51 2.1.1. ConstantsandVariables ................................ 53 2.1.1.1. Enumerations ...................................... 54 2.2. BooleanValuesandExpressions .............................. 56 2.3. NumericTypes .............................................. 57 2.3.1. IntegerTypes .......................................... 59 2.3.1.1. BigIntegers........................................ 61 2.3.2. Floating-PointTypes .................................... 64 2.3.2.1. ComplexTypes .................................... 70 2.4. Example:Statistics .......................................... 72 2.4.1. ImplementingSimpleStatisticsFunctions................ 73 2.4.2. ImplementingaBasicHTTPServer ...................... 75 2.5. Exercises .................................................... 78 Chapter3. Strings ................................................ 81 3.1. Literals,Operators,andEscapes .............................. 83 3.2. ComparingStrings .......................................... 86 3.3. CharactersandStrings ...................................... 87 3.4. IndexingandSlicingStrings .................................. 90 3.5. StringFormattingwiththeFmtPackage ...................... 93 3.5.1. FormattingBooleans .................................... 97 3.5.2. FormattingIntegers .................................... 98 3.5.3. FormattingCharacters .................................. 99 3.5.4. FormattingFloating-PointNumbers .................... 100 3.5.5. FormattingStringsandSlices............................ 101 3.5.6. FormattingforDebugging .............................. 103 3.6. OtherString-RelatedPackages .............................. 106 3.6.1. TheStringsPackage .................................... 107 3.6.2. TheStrconvPackage .................................... 113 3.6.3. TheUtf8Package ...................................... 117 3.6.4. TheUnicodePackage .................................... 118 3.6.5. TheRegexpPackage .................................... 120 3.7. Example:M3u2pls .......................................... 130 3.8. Exercises .................................................... 135 Chapter4. CollectionTypes ...................................... 139 4.1. Values,Pointers,andReferenceTypes ........................ 140 4.2. ArraysandSlices ............................................ 148 4.2.1. IndexingandSlicingSlices .............................. 153 4.2.2. IteratingSlices.......................................... 154 4.2.3. ModifyingSlices ........................................ 156 4.2.4. SortingandSearchingSlices ............................ 160 4.3. Maps ........................................................ 164 4.3.1. CreatingandPopulatingMaps .......................... 166 4.3.2. MapLookups .......................................... 168 4.3.3. ModifyingMaps ........................................ 169 4.3.4. Key-OrderedMapIteration .............................. 170 4.3.5. MapInversion .......................................... 170 4.4. Examples .................................................... 171 4.4.1. Example:GuessSeparator .............................. 171 4.4.2. Example:WordFrequencies.............................. 174 4.5. Exercises .................................................... 180 Chapter5. ProceduralProgramming ............................ 185 5.1. StatementBasics ............................................ 186 5.1.1. TypeConversions ...................................... 190 5.1.2. TypeAssertions ........................................ 191 5.2. Branching .................................................. 192 5.2.1. If Statements .......................................... 192 5.2.2. SwitchStatements ...................................... 195 5.2.2.1. ExpressionSwitches................................ 195 5.2.2.2. TypeSwitches...................................... 197 5.3. LoopingwithForStatements ................................ 203 5.4. CommunicationandConcurrencyStatements ................ 205 5.4.1. SelectStatements ...................................... 209 5.5. Defer,Panic,andRecover .................................... 212 5.5.1. PanicandRecover ...................................... 213 5.6. CustomFunctions ............................................ 219 5.6.1. FunctionArguments .................................... 220 5.6.1.1. FunctionCallsasFunctionArguments .............. 220 5.6.1.2. VariadicFunctions ................................ 221 5.6.1.3. FunctionswithMultipleOptionalArguments ........ 222 5.6.2. Theinit()andmain()Functions .......................... 224 5.6.3. Closures ................................................ 225 5.6.4. RecursiveFunctions .................................... 227 5.6.5. ChoosingFunctionsatRuntime .......................... 230 5.6.5.1. BranchingUsingMapsandFunctionReferences .... 230 5.6.5.2. DynamicFunctionCreation ........................ 231 5.6.6. GenericFunctions ...................................... 232 5.6.7. HigherOrderFunctions ................................ 238 5.6.7.1. MemoizingPureFunctions ........................ 241 5.7. Example: IndentSort ........................................ 244 5.8. Exercises .................................................... 250 Chapter6. Object-OrientedProgramming ...................... 253 6.1. KeyConcepts ................................................ 254 6.2. CustomTypes................................................ 256 6.2.1. AddingMethods ........................................ 258 6.2.1.1. OverridingMethods ................................ 261 6.2.1.2. MethodExpressions ................................ 263 6.2.2. ValidatedTypes ........................................ 263 6.3. Interfaces.................................................... 265 6.3.1. InterfaceEmbedding .................................... 270 6.4. Structs ...................................................... 275 6.4.1. StructAggregationandEmbedding ...................... 275 6.4.1.1. EmbeddingValues ................................ 276 6.4.1.2. EmbeddingAnonymousValuesThatHaveMethods .. 277 6.4.1.3. EmbeddingInterfaces .............................. 279 6.5. Examples .................................................... 282 6.5.1. Example:FuzzyBool—ASingle-ValuedCustomType ...... 282 6.5.2. Example:Shapes—AFamilyof CustomTypes ............ 289 6.5.2.1. Package-LevelConvenienceFunctions .............. 289 6.5.2.2. AHierarchyof EmbeddedInterfaces ................ 294 6.5.2.3. FreelyComposableIndependentInterfaces .......... 294 6.5.2.4. ConcreteTypesandMethods ........................ 295 6.5.3. Example:OrderedMap—AGenericCollectionType ...... 302 6.6. Exercises .................................................... 311 Chapter7. ConcurrentProgramming ............................ 315 7.1. KeyConcepts ................................................ 317 7.2. Examples .................................................... 322 7.2.1. Example:Filter ........................................ 322 7.2.2. Example:ConcurrentGrep .............................. 326 7.2.3. Example:Thread-SafeMap .............................. 334 7.2.4. Example:ApacheReport ................................ 341 7.2.4.1. SynchronizingwithaSharedThread-SafeMap ...... 341 7.2.4.2. SynchronizingwithaMutex-ProtectedMap ........ 345 7.2.4.3. SynchronizingbyMergingLocalMapsviaChannels 347 7.2.5. Example:FindDuplicates .............................. 349 7.3. Exercises .................................................... 357 Chapter8. FileHandling .......................................... 361 8.1. CustomDataFiles .......................................... 362 8.1.1. HandlingJSONFiles .................................... 365 8.1.1.1. WritingJSONFiles ................................ 366 8.1.1.2. ReadingJSONFiles ................................ 368 8.1.2. HandlingXMLFiles .................................... 371 8.1.2.1. WritingXMLFiles ................................ 371 8.1.2.2. ReadingXMLFiles ................................ 375 8.1.3. HandlingPlainTextFiles................................ 377 8.1.3.1. WritingPlainTextFiles ............................ 378 8.1.3.2. ReadingPlainTextFiles ............................ 380 8.1.4. HandlingGoBinaryFiles................................ 385 8.1.4.1. WritingGoBinaryFiles ............................ 385 8.1.4.2. ReadingGoBinaryFiles ............................ 386 8.1.5. HandlingCustomBinaryFiles .......................... 387 8.1.5.1. WritingCustomBinaryFiles ...................... 388 8.1.5.2. ReadingCustomBinaryFiles ...................... 392 8.2. ArchiveFiles ................................................ 397 8.2.1. CreatingZipArchives .................................. 397 8.2.2. CreatingOptionallyCompressedTarballs ................ 399 8.2.3. UnpackingZipArchives ................................ 401 8.2.4. UnpackingOptionallyCompressedTarballs .............. 403 8.3. Exercises .................................................... 405 Chapter9. Packages .............................................. 407 9.1. CustomPackages ............................................ 408 9.1.1. CreatingCustomPackages .............................. 408 9.1.1.1. Platform-SpecificCode.............................. 410 9.1.1.2. DocumentingPackages ............................ 411 9.1.1.3. UnitTestingandBenchmarkingPackages .......... 414 9.1.2. ImportingPackages .................................... 416 9.2. Third-PartyPackages ........................................ 417 9.3. ABrief Surveyof Go’sCommands ............................ 418 9.4. ABrief Surveyof theGoStandardLibrary .................... 419 9.4.1. ArchiveandCompressionPackages ...................... 419 9.4.2. BytesandString-RelatedPackages ...................... 419 9.4.3. CollectionPackages .................................... 421 9.4.4. File,OperatingSystem,andRelatedPackages ............ 423 9.4.4.1. FileFormat-RelatedPackages ...................... 424 9.4.5. Graphics-RelatedPackages .............................. 425 9.4.6. MathematicsPackages .................................. 425 9.4.7. MiscellaneousPackages ................................ 425 9.4.8. NetworkingPackages .................................. 427 9.4.9. TheReflectPackage .................................... 427 9.5. Exercises .................................................... 431 AppendixA. Epilogue ............................................ 435 AppendixB. TheDangersof SoftwarePatents .................. 437 AppendixC. SelectedBibliography .............................. 441 Index .............................................................. 443 Tables 2.1. Go’sKeywords .............................................. 52 2.2. Go’sPredefinedIdentifiers .................................. 52 2.3. BooleanandComparisonOperators .......................... 57 2.4. ArithmeticOperatorsApplicabletoAllBuilt-InNumbers ...... 59 2.5. Go’sIntegerTypesandRanges .............................. 60 2.6. ArithmeticOperatorsApplicableOnlytoBuilt-InIntegerTypes 60 2.7. Go’sFloating-PointTypes .................................... 64 2.8. TheMathPackage’sConstantsandFunctions#1 .............. 65 2.9. TheMathPackage’sConstantsandFunctions#2 .............. 66 2.10. TheMathPackage’sConstantsandFunctions#3 .............. 67 2.11. TheComplexMathPackage’sFunctions ...................... 71 3.1. Go’sStringandCharacterEscapes .......................... 84 3.2. StringOperations .......................................... 85 3.3. TheFmtPackage’sPrintFunctions .......................... 94 3.4. TheFmtPackage’sVerbs .................................... 95 3.5. TheFmtPackage’sVerbModifiers ............................ 96 3.6. TheStringsPackage’sFunctions#1 .......................... 108 3.7. TheStringsPackage’sFunctions#2 .......................... 109 3.8. TheStrconvPackage’sFunctions#1 .......................... 114 3.9. TheStrconvPackage’sFunctions#2 .......................... 115 3.10. TheUtf8Package’sFunctions ................................ 118 3.11. TheUnicodePackage’sFunctions ............................ 119 3.12. TheRegexpPackage’sFunctions ............................ 121 3.13. TheRegexpPackage’sEscapeSequences .................... 121 3.14. TheRegexpPackage’sCharacterClasses .................... 122 3.15. TheRegexpPackage’sZero-WidthAssertions ................ 122 3.16. TheRegexpPackage’sQuantifiers ............................ 123 3.17. TheRegexpPackage’sFlagsandGroups ...................... 123 3.18. The*regexp.RegexpType’sMethods#1 ...................... 124 3.19. The*regexp.RegexpType’sMethods#2 ...................... 125 4.1. SliceOperations ............................................ 151

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.