EPUB is an open, industry-standard format for e-books. However, support for EPUB and its many features varies across reading devices and applications. Use your device or app settings to customize the presentation to your liking. Settings that you can customize often include font, font size, single or double column, landscape or portrait mode, and figures that you can click or tap to enlarge. For additional information about the settings and features on your reading device or app, visit the device manufacturers Web site.
Many titles include programming code or configuration examples. To optimize the presentation of these elements, view the e-book in single-column, landscape mode and adjust the font size to the smallest setting. In addition to presenting code and configurations in the reflowable text format, we have included images of the code that mimic the presentation found in the print book; therefore, where the reflowable format may compromise the presentation of the code listing, you will see a Click here to view code image link. Click the link to view the print-fidelity code image. To return to the previous page viewed, click the Back button on your device or app.
The Go Programming Language
Alan A. A. Donovan
Google Inc.
Brian W. Kernighan
Princeton University
New York Boston Indianapolis San Francisco
Toronto Montreal London Munich Paris Madrid
Capetown Sydney Tokyo Singapore Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals.
The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein.
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the United States, please contact .
Visit us on the Web: informit.com/aw
Library of Congress Control Number: 2015950709
Copyright 2016 Alan A. A. Donovan & Brian W. Kernighan
All rights reserved. Printed in the United States of America. This publication is protected by copyright, 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. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, 200 Old Tappan Road, Old Tappan, New Jersey 07675, or you may fax your request to (201) 236-3290.
Front cover: Millau Viaduct, Tarn valley, southern France. A paragon of simplicity in modern engineering design, the viaduct replaced a convoluted path from capital to coast with a direct route over the clouds. Jean-Pierre Lescourret/Corbis.
Back cover: the original Go gopher. 2009 Rene French. Used under Creative Commons Attributions 3.0 license.
Typeset by the authors in Minion Pro, Lato, and Consolas, using Go, groff, ghostscript, and a host of other open-source Unix tools. Figures were created in Google Drawings.
ISBN-13: 978-0-13-419044-0
ISBN-10: 0-13-419044-0
Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.
Second printing, December 2015
For Leila and Meg
Contents
Preface
Go is an open source programming language that makes iteasy to build simple, reliable, and efficient software.(From the Go web site at golang.org
)
Go was conceived in September 2007 by Robert Griesemer, Rob Pike, andKen Thompson, all at Google, and was announced in November 2009. Thegoals of the language and itsaccompanying tools were to be expressive, efficient in bothcompilation and execution, and effective in writing reliable androbust programs.
Go bears a surface similarity to C and, like C, is a toolfor professional programmers, achieving maximum effect withminimum means. But it is much more than an updated version of C. Itborrows and adapts good ideas from many other languages, while avoidingfeatures that have led to complexity and unreliable code. Itsfacilities for concurrency are new and efficient, and itsapproach to data abstraction and object-oriented programming isunusually flexible.It has automatic memory management or garbage collection.
Go is especially well suited for building infrastructure likenetworked servers, and tools and systems for programmers, but it istruly a general-purpose language and finds use in domains asdiverse as graphics, mobile applications, and machine learning.It has become popular as a replacement for untyped scripting languagesbecause it balances expressiveness with safety: Go programs typicallyrun faster than programs written in dynamic languages and suffer farfewer crashes due to unexpected type errors.
Go is an open-source project, so source code for its compiler,libraries, and tools is freely available toanyone. Contributions to the project come froman active worldwide community.Go runs on Unix-like systemsLinux, FreeBSD, OpenBSD,Mac OS Xand on Plan 9 and Microsoft Windows.Programs written in one of these environmentsgenerally work without modification on the others.
This book is meant to help you start using Go effectively right awayand to use it well, taking full advantage of Goslanguage features and standard libraries to write clear, idiomatic, andefficient programs.
The Origins of Go
Like biologicalspecies, successful languages beget offspring that incorporate theadvantages of their ancestors; interbreeding sometimes leads tosurprising strengths; and, very occasionally, a radical new featurearises without precedent.We can learn a lot about why a language is the way it is and whatenvironment it has been adapted for by looking at these influences.
The figure below shows the most important influences ofearlier programming languages on the design of Go.
Go is sometimes described as a C-like language, or as C for the 21stcentury. From C, Go inherited its expression syntax, control-flowstatements, basic data types, call-by-value parameter passing,pointers, and above all, Cs emphasis on programs that compileto efficient machine code and cooperate naturally with theabstractions of current operating systems.
But there are other ancestors in Gos family tree. One major stream ofinfluence comes from languages by Niklaus Wirth, beginning with Pascal.Modula-2 inspired the package concept.Oberon eliminated the distinction between module interface files andmodule implementation files.Oberon-2 influenced the syntax for packages, imports, and declarations,and Object Oberon provided the syntax for method declarations.