Transcription of Programming in Go - pearsoncmg.com
1 Programming in GoThe Developer s Library Series from Addison-Wesley provides practicing programmers with unique, high-quality references and tutorials on the latest Programming languages and technologies they use in their daily work. All books in the Developer s Library are written by expert technology practitioners who are exceptionally skilled at organizing and presenting information in a way that s useful for other programmers. Developer s Library books cover a wide range of topics, from open-source Programming languages and databases, Linux Programming , Microsoft, and Java, to Web development, social networking platforms, Mac/iPhone Programming , and Android for a complete list of available productsDeveloper s Library SeriesProgramming in GoCreating Applications for the 21st CenturyMark SummerfieldUpper Saddle River.
2 NJ Boston Indianapolis San Franciscop New York Toronto Montreal London Munich Paris MadridpCapetown Sydney Tokyo Singapore Mexico CityMany of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and the publisher was aware of a trademarkclaim, the designations have been printed with initial capital letters or in all author and publisher have taken care in the preparation of this book, but make no expressed orimplied warranty of any kind and assume no responsibility for errors or omissions.
3 No liability isassumed for incidental or consequential damages in connection with or arising out of the use of theinformation or programs contained publisher offers excellent discounts on this book when ordered in quantity for bulk purchasesor special sales, which may include electronic versions and/or custom covers and content particularto your business, training goals, marketing focus, and branding interests. For more information,please contact: Corporate and Government Sales (800) 382-3419 sales outside the United States, please contact: International Sales us on the Web: of Congress Cataloging-in-Publication DataSummerfield, Mark.
4 Programming in Go : creating applications for the 21st century / Mark Summerfield. Includes bibliographical references and index. ISBN 978-0-321-77463-7 (pbk. : alk. paper)1. Go (Computer program language ) 2. Computer Programming 3. Application software Development I. Title. 2012 3 dc232012001914 Copyright 2012 Qtrac rights reserved. Printed in the United States of America. This publication is protected bycopyright, and permission must be obtained from the publisher prior to any prohibited reproduction,storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical,photocopying, recording, or likewise.
5 To obtain permission to use material from this work, pleasesubmit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, UpperSaddle River, New Jersey 07458, or you may fax your request to (201) : 978-0-321-77463-7 ISBN-10:0-321-77463-9 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, printing, April 2012 This book is dedicated toJasmin Blanchette and Trenton SchulzThis page intentionally left blank Contents at a 1. An Overview in Five 7 Chapter 2. Booleans and 51 Chapter 3.
6 81 Chapter 4. Collection 139 Chapter 5. Procedural 185 Chapter 6. Object-Oriented 253 Chapter 7. Concurrent 315 Chapter 8. File 361 Chapter 9. 407 Appendix A. 435 Appendix B. The Dangers of Software 437 Appendix C. Selected page intentionally left blank ..1 The Structure of the 5 Chapter Overview in Five Getting Editing, Compiling, and Hello Who?.. Big Digits Two-Dimensional Stack Custom Types with Americanise Files, Maps, and Polar to Cartesian 48 Chapter and Constants and Boolean Values and Numeric Integer Big Floating-Point Complex Example.
7 Implementing Simple Statistics Implementing a Basic HTTP 78ixChapter Literals, Operators, and Comparing Characters and Indexing and Slicing String Formatting with the Fmt Formatting Formatting Formatting Formatting Floating-Point Formatting Strings and Formatting for Other String-Related The Strings The Strconv The Utf8 The Unicode The Regexp Example: 135 Chapter Values, Pointers, and Reference Arrays and Indexing and Slicing Iterating Modifying Sorting and Searching Creating and Populating Map Modifying Key-Ordered Map Map Example: Guess Example.
8 Word 180xChapter Statement Type Type If Switch Expression Type Looping with For Communication and Concurrency Select Defer, Panic, and Panic and Custom Function Function Calls as Function Variadic Functions with Multiple Optional The init() and main() Recursive Choosing Functions at Branching Using Maps and Function Dynamic Function Generic Higher Order Memoizing Pure Example: Indent 250 Chapter Key Custom Adding Overriding Method Validated Interface Struct Aggregation and Embedding Embedding Anonymous Values That Have Methods.
9 Embedding Example: FuzzyBool A Single-Valued Custom Example: Shapes A Family of Custom Package-Level Convenience A Hierarchy of Embedded Freely Composable Independent Concrete Types and Example: Ordered Map A Generic Collection 311 Chapter Key Example: Example: Concurrent Example: Thread-Safe Example: Apache Synchronizing with a Shared Thread-Safe Synchronizing with a Mutex-Protected Synchronizing by Merging Local Maps via Channels Example.
10 Find 357 Chapter Custom Data Handling JSON Writing JSON Reading JSON Handling XML Writing XML Reading XML Handling Plain Text Writing Plain Text Reading Plain Text Handling Go Binary Writing Go Binary Reading Go Binary Handling Custom Binary Writing Custom Binary Reading Custom Binary Archive Creating Zip Creating Optionally Compressed Unpacking Zip Unpacking Optionally Compressed 405 Chapter Custom Creating Custom Platform-Specific Documenting Unit Testing and Benchmarking Importing Third-Party A Brief Survey of Go s A Brief Survey of the Go Standard Archive and Compression Bytes and String-Related Collection File, Operating System.