The Go Programming Language Phrasebook
David Chisnall
Upper Saddle River, NJ Boston Indianapolis San Francisco
New York Toronto Montreal London Munich Paris Madrid
Cape Town 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 author 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.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom 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) 382-3419
For sales outside the United States, please contact:
International Sales
Visit us on the Web: informit.com/aw
Library 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 2012
005.3dc23
2012000478
Copyright 2012 Pearson Education, Inc.
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, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290.
ISBN-13: 978- 0-321-81714-3
ISBN-10: 0-321-81714-1
Text printed in the United States on recycled paper at Edwards Brothers Malloy in Ann Arbor, Michigan.
First printing: March 2012
Editor-in-Chief
Mark Taub
Acquisitions Editor
Debra Williams
Cauley
Marketing Manager
Stephane Nakib
Managing Editor
Kristy Hart
Project Editor
Anne Goebel
Copy Editor
Gayle Johnson
Publishing Coordinator
Andrea Bledsoe
Cover Designer
Gary Adair
Senior Compositor
Gloria Schurick
About the Author
David 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 OpenStep and Cocoa APIs. He is an active contributor to GNUstep and is the original author and maintainer of the GNUstep Objective-C 2 runtime library and the associated compiler support in the Clang compiler. He is also a FreeBSD committer working various aspects of the toolchain, including being responsible for the new C++ stack.
After completing his PhD, David hid in academia for a while, studying the history of programming languages. He finally escaped when he realized that there were places off campus with an equally good view of the sea and without the requirement to complete quite so much paperwork. He occasionally returns to collaborate on projects involving modeling the semantics of dynamic languages.
When not writing or programming, David enjoys dancing Argentine tango and Cuban salsa, playing badminton and ultimate frisbee, and cooking.
Acknowledgments
The first person Id like to thank is Mark Summerfield, author of Programming in Go: Creating Applications for the 21st Century. If you finish this book and want to learn more, Id recommend you pick up a copy. Mark was the person responsible for making me look at Go in the first place.
The next person I need to thank is Yoshiki Shibata. Yoshiki has been working on the Japanese translation of this book and, in doing so, has sent me countless emails highlighting areas that could be improved. If you enjoy reading this book then Yoshiki deserves a lot of the credit.
Finally, I need to thank everyone else who was involved in bringing this book from my text editor to your hands. A lot of people have earned some credit along the way. In particular, Debra Williams-Cauley, who masterminded the project, and Anne Goebel, who shepherded the book from a draft manuscript to the version you now hold.
1. Introducing Go
When learning a new language, there are three things that you need to understand. The first and most important is the abstract model that the language presents. The next is the concrete syntax. Finally, you need to learn your way around the standard libraries and the common idioms of the language.
This chapter will look at the abstract model that Go presents to programmers. If you want to dive straight into real examples, skip to the next chapter, which covers the concrete syntax. The rest of the book will cover highlights from the Go standard library and the various idioms that you will find common in Go code.
Go and C
In the late 60s, a small team at the Bell Telephone Laboratories wrote a simple operating system called UNICS, a very lightweight system inspired by the MULTICS project, on the PDP-7 minicomputer that they had access to. When they wanted to port it to another system, they had to rewrite all of the code, which was written in PDP-7 assembly language.
To make the transition easier, they wanted to be able to share as much code as possible between different versions. They needed a language that was sufficiently low-level that a simple compiler (the only kind that existed in the 60s) could generate efficient machine code from it, yet which hid most of the irrelevant details of the target machine. BCPL was close, but it was too complex in some areas and lacked some required features in others.
Dennis Ritchie created the C programming language as a derivative of BCPL, and eventually most of the PDP-11 version of UNIX was rewritten in it. When UNIX was ported to the VAX, they just needed to retarget the compiler and write a small amount of very low-level assembly code. The majority of the system could be recompiled without modification.
Since its initial public release in 1978, C has become a very popular language. It is the de facto standard low-level language for programming these days, and it even finds use in a significant amount of application development.
The point of a low-level language is to provide an abstract machine model to the programmer that closely reflects the architecture of the concrete machines that it will target. There is no such thing as a universal low-level language: a language that closely represents the architecture of a PDP-11 will not accurately reflect something like a modern GPU or even an old B5000 mainframe. The attraction of C has been that, in providing an abstract model similar to a PDP-11, it is similar to most cheap consumer CPUs.