Go Brain Teasers
Exercise Your Mind
by Miki Tebeka
Version: P1.0 (September 2021)
Copyright 2021 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
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 Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/#about-ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/d-gobrain, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Dave Rankin (CEO) Janet Furlow (COO) Tammy Coron (Managing Editor) Margaret Eldridge (Development Editor) Jennifer Whipple (Copy Editor) Potomac Indexing, LLC (Indexing) Gilson Graphics (Layout) Andy Hunt and Dave Thomas (Founders)
For customer support, please contact .
For international rights, please contact .
To Sharon, who suffered me in quarantine, and the twenty years before that.
Table of Contents
Copyright 2021, The Pragmatic Bookshelf.
Early Praise for Go Brain Teasers
In Go Brain Teasers , Miki has prepared a set of tests that seek not tosimply baffle but to enlighten. Go Brain Teasers provides the readerwith the opportunity to learn the why behind that what!?!
Dave Cheney |
Staff Software Engineer, GitHub |
Go Brain Teasers should be renamed Go Mind Blowers .
David Bordeynik |
Software Architect, NVIDIA |
Go Brain Teasers is the completion for the Tour of Go that you were looking for.It suits anyone who is writing Go and wants to grasp the language in a fun and productive way.
Yoni Davidson |
Software Engineer and Data Architect, Bond |
I see after having started reading that the exercises are very well-explained.Thanks! Been enjoying your book, by the way.
Jared Davis |
Owner / Software Engineer, 1ijk Development, LLC |
Acknowledgments
Im grateful for every contribution, from finding bugs to fixing grammar toletting me work in peace.
Here is a list of people who helped; my apologies to anyone I forgot:
Adi Tebeka for her proofreading and comments.
Dan Allen for his help in the asciidoctor forums. I used the wonderful asciidoctor to write the initial version of this book.
Dave Cheney for the foreword.
David Bordeynik for his comments.
Egon Elbre who drew all these wonderful gophers and placed them in CC0 license.
Elad Eyal for proofreading.
Eliran Bivas for proofreading and comments.
Jared David for his quote.
Ran Tavory for his comments.
Yoni Davidson for his comments.
Im also grateful to Brad Fitzpartick and others who post Go pop quizzes andgave me many ideas for these brain teasers.
Copyright 2021, The Pragmatic Bookshelf.
Preface
The Go programming language is a simple one, but like all other languages, ithas its quirks. This book uses these quirks as a teaching opportunity. Byunderstanding the gaps in your knowledge, youll become better at what you do.
Theres a lot of research showing that people who make mistakes during thelearning process learn better than people who dont. If you use this approachat work when fixing bugs, youll find you enjoy bug hunting more and become abetter developer after each bug you fix.
These teasers will help you avoid mistakes. Some of the teasers are from my ownexperience shipping bugs to production, and some are from others doing the same.
Teasers are fun! We geeks love puzzles and solving them. You can also use theseteasers to impress your coworkers, have knowledge competitions, and becomebetter together.
Many of these brain teasers are from quizzes I gave at conferences andmeetups. Ive found that people highly enjoy them, and they tend to liventhe room.
At the beginning of each chapter, Ill show you a short Go program and askyou to guess the output. The possible answers can be the following:
- Wont compile
- Panic
- Deadlock
- Some output (e.g., [1 2 3] )
Go Version |
---|
| Ive used Go version 1.14.1 to run the code; the output might change in future versions. |
Before moving on to the answer and the explanation, go ahead and guessthe output. After guessing the output, I encourage you to run the codeand see the output yourself; only then proceed to read the solution andthe explanation. Ive been teaching programming for many years andfound this course of action to be highly effective.