Go Standard Library Cookbook Over 120 specific ways to make full use of the standard library components in Golang Radomír Sohlich BIRMINGHAM - MUMBAI Go Standard Library Cookbook Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Merint Mathew Acquisition Editor: Aiswarya Narayanan Content Development Editor: Anugraha Arunagiri Technical Editor: Subhalaxmi Nadar Copy Editor: Safis Editing Project Coordinator: Ulhas Kambali Proofreader: Safis Editing Indexer: Rekha Nair Graphics: Tania Dutta Production Coordinator: Arvindkumar Gupta First published: February 2018 Production reference: 1230218 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78847-527-3 www.packtpub.com mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks. Contributors About the author Radomír Sohlich received the master's degree in Applied Informatics from Faculty of Applied Informatics at Tomas Bata University in Zlín. After that, he got a job in a start-up company as a software developer and worked on various projects, usually based on the Java platform. Currently, he continues a software developer career as a contractor for a large international company. In 2015, he fell in love with Go and kept exploring the endless power and possibilities of the language. He is passionate about learning new approaches and technology and feels the same about sharing the knowledge with others. I'd like to thank my beloved wife and kids for the time they gave me for creating this book. The next big thank you belongs to Mert Serin, who agreed to review the book and give a feedback on the content. About the reviewer Mert Serin was born in Izmir in 1993, graduated from Hacettepe University in 2016, and has worked on iOS development since his third year in university. He is currently working at USIT Inc., a start-up based in Atlanta, GE, as a full stack developer. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Table of Contents Preface 1 Chapter 1: Interacting with the Environment 6 Introduction 6 Retrieving the Golang version 7 Getting ready 7 How to do it... 7 How it works... 8 Accessing program arguments 9 How to do it... 9 How it works... 10 There's more… 10 Creating a program interface with the flag package 11 How to do it... 11 How it works… 13 There's more… 14 Getting and setting environment variables with default values 14 How to do it… 14 How it works… 17 Retrieving the current working directory 18 How to do it... 18 How it works… 19 Getting the current process PID 20 How to do it… 20 How it works… 21 Handling operating system signals 21 How to do it… 22 How it works… 23 Calling an external process 24 Getting ready 24 How to do it… 25 Table of Contents How it works… 27 See also 28 Retrieving child process information 28 Getting ready 28 How to do it… 28 How it works… 31 See also 31 Reading/writing from the child process 31 Getting ready 31 How to do it… 32 How it works… 38 Shutting down the application gracefully 39 How to do it… 39 How it works… 42 See also 42 File configuration with functional options 42 How to do it... 42 How it works... 45 Chapter 2: Strings and Things 46 Introduction 46 Finding the substring in a string 47 How to do it... 47 How it works... 48 See also 48 Breaking the string into words 49 How to do it... 49 How it works... 52 There's more... 53 Joining the string slice with a separator 53 How to do it... 53 How it works... 55 There's more... 56 Concatenating a string with writer 56 How to do it... 56 How it works... 58 [ ii ] Table of Contents There's more... 58 Aligning text with tabwriter 59 How to do it... 60 How it works... 61 Replacing part of the string 61 How to do it... 61 How it works... 63 There's more... 64 Finding the substring in text by the regex pattern 64 How to do it... 64 How it works... 65 See also 66 Decoding a string from the non-Unicode charset 66 How to do it... 66 How it works... 68 Controlling case 69 How to do it... 69 How it works... 71 Parsing comma-separated data 71 How to do it... 72 How it works... 74 Managing whitespace in a string 75 How to do it... 75 How it works... 76 See also 77 Indenting a text document 77 How to do it... 78 How it works... 79 See also 79 Chapter 3: Dealing with Numbers 80 Introduction 80 Converting strings to numbers 81 How to do it... 81 How it works... 82 Comparing floating-point numbers 83 [ iii ] Table of Contents How to do it... 83 How it works... 85 Rounding floating-point numbers 86 How to do it... 86 How it works... 87 Floating-point arithmetics 88 How to do it... 88 How it works... 89 There's more... 89 See also 90 Formatting numbers 90 How to do it... 90 How it works... 92 There's more... 93 Converting between binary, octal, decimal, and hexadecimal 93 How to do it... 93 How it works... 95 Formatting with the correct plurals 95 Getting ready 95 How to do it... 95 How it works... 97 There's more... 97 Generating random numbers 98 How to do it... 98 How it works... 99 Operating complex numbers 100 How to do it... 100 How it works... 101 Converting between degrees and radians 102 How to do it... 102 How it works... 103 Taking logarithms 104 How to do it... 104 How it works... 105 Generating checksums 105 [ iv ]