Statistics
72
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-11-25

AuthorAdam Freeman

Best-selling author Adam Freeman explains how to get the most from Go, starting from the basics and building up to the most advanced and sophisticated features. You will learn how Go builds on a simple and consistent type system to create a comprehensive and productive development experience that produces fast and robust applications that run across platforms. Go, also known as Golang, is the concise and efficient programming language designed by Google for creating high-performance, cross-platform applications. Go combines strong static types with simple syntax and a comprehensive standard library to increase programmer productivity, while still supporting features such as concurrent/parallel programming. Each topic is covered in a clear, concise, no-nonsense approach that is packed with the details you need to learn to be truly effective. Chapters include common problems and how to avoid them.

Tags
No tags
ISBN: 1484273559
Publisher: Apress
Publish Year: 2022
Language: 英文
Pages: 1078
File Format: PDF
File Size: 13.1 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Pro Go The Complete Guide to Programming Reliable and Efficient Software Using Golang Adam Freeman
Pro Go: The Complete Guide to Programming Reliable and Efficient Software Using Golang ISBN-13 (pbk): 978-1-4842-7354-8 ISBN-13 (electronic): 978-1-4842-7355-5 https://doi.org/10.1007/978-1-4842-7355-5 Copyright © 2022 by Adam Freeman This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: Joan Murray Development Editor: Laura Berendson Editorial Operations Manager: Mark Powers Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004, U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail editorial@apress.com; for reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub. For more detailed information, please visit www.apress.com/source- code. Printed on acid-free paper Adam Freeman London, UK 
Dedicated to my lovely wife, Jacqui Griffyth. (And also to Peanut.)
v ■Part I: Understanding the Go Language ���������������������������������������������� 1 ■Chapter 1: Your First Go Application ��������������������������������������������������������������������� 3 Setting the Scene ������������������������������������������������������������������������������������������������������������� 3 Installing the Development Tools �������������������������������������������������������������������������������������� 3 Installing Git �������������������������������������������������������������������������������������������������������������������������������������������� 4 Selecting a Code Editor �������������������������������������������������������������������������������������������������������������������������� 4 Creating the Project ���������������������������������������������������������������������������������������������������������� 5 Defining a Data Type and a Collection ������������������������������������������������������������������������������ 6 Creating HTML Templates������������������������������������������������������������������������������������������������� 8 Loading the Templates �������������������������������������������������������������������������������������������������������������������������� 11 Creating the HTTP Handlers and Server ������������������������������������������������������������������������� 15 Writing the Form Handling Function������������������������������������������������������������������������������� 18 Handling the Form Data ������������������������������������������������������������������������������������������������������������������������ 22 Adding Data Validation ��������������������������������������������������������������������������������������������������� 23 Summary ������������������������������������������������������������������������������������������������������������������������ 25 Table of Contents About the Author �������������������������������������������������������������������������������������������������xxvii About the Technical Reviewer ������������������������������������������������������������������������������xxix
vi ■Chapter 2: Putting Go in Context ������������������������������������������������������������������������� 27 Why Should You Learn Go? ��������������������������������������������������������������������������������������������� 27 What’s the Catch? ���������������������������������������������������������������������������������������������������������� 27 Is It Really That Bad? ������������������������������������������������������������������������������������������������������ 28 What Do You Need to Know? ������������������������������������������������������������������������������������������ 28 What Is the Structure of This Book? ������������������������������������������������������������������������������� 28 Part 1: Understanding the Go Language ����������������������������������������������������������������������������������������������� 28 Part 2: Using the Go Standard Library �������������������������������������������������������������������������������������������������� 28 Part 3: Applying Go ������������������������������������������������������������������������������������������������������������������������������� 28 What Doesn’t This Book Cover? ������������������������������������������������������������������������������������� 28 What If You Find an Error in the Book? ��������������������������������������������������������������������������� 29 Are There Lots of Examples? ������������������������������������������������������������������������������������������ 29 What Software Do You Need for the Examples? ������������������������������������������������������������� 31 What Platforms Will the Examples Run On? ������������������������������������������������������������������� 31 What If You Have Problems Following the Examples? ���������������������������������������������������� 31 Where Can You Get the Example Code? ������������������������������������������������������������������������� 32 Why Do Some of the Examples Have Odd Formatting? �������������������������������������������������� 32 How Do I Contact the Author? ���������������������������������������������������������������������������������������� 32 What If I Really Enjoyed This Book? ������������������������������������������������������������������������������� 32 What If This Book Has Made Me Angry and I Want to Complain? ����������������������������������� 32 Summary ������������������������������������������������������������������������������������������������������������������������ 33 ■Chapter 3: Using the Go Tools ������������������������������������������������������������������������������ 35 Using the Go Command �������������������������������������������������������������������������������������������������� 35 Creating a Go Project ������������������������������������������������������������������������������������������������������ 36 Understanding the Package Declaration ���������������������������������������������������������������������������������������������� 37 Understanding the Import Statement ��������������������������������������������������������������������������������������������������� 37 Understanding the Function ����������������������������������������������������������������������������������������������������������������� 38 Understanding the Code Statement ������������������������������������������������������������������������������������������������������ 38 ■ Table of ConTenTs
vii Compiling and Running Source Code ����������������������������������������������������������������������������� 40 Cleaning Up ������������������������������������������������������������������������������������������������������������������������������������������ 41 Using the Go Run Command ����������������������������������������������������������������������������������������������������������������� 41 Defining a Module ��������������������������������������������������������������������������������������������������������������������������������� 41 Debugging Go Code �������������������������������������������������������������������������������������������������������� 42 Preparing for Debugging ���������������������������������������������������������������������������������������������������������������������� 43 Using the Debugger ������������������������������������������������������������������������������������������������������������������������������ 44 Using a Delve Editor Plugin������������������������������������������������������������������������������������������������������������������� 46 Linting Go Code �������������������������������������������������������������������������������������������������������������� 47 Using the Linter ������������������������������������������������������������������������������������������������������������������������������������ 48 Disabling Linter Rules ��������������������������������������������������������������������������������������������������������������������������� 50 Fixing Common Problems in Go Code ���������������������������������������������������������������������������� 53 Formatting Go Code �������������������������������������������������������������������������������������������������������� 56 Summary ������������������������������������������������������������������������������������������������������������������������ 57 ■Chapter 4: Basic Types, Values, and Pointers ������������������������������������������������������ 59 Preparing for This Chapter ��������������������������������������������������������������������������������������������� 60 Using the Go Standard Library ��������������������������������������������������������������������������������������� 61 Understanding the Basic Data Types ������������������������������������������������������������������������������ 63 Understanding Literal Values ���������������������������������������������������������������������������������������������������������������� 64 Using Constants ������������������������������������������������������������������������������������������������������������� 65 Understanding Untyped Constants ������������������������������������������������������������������������������������������������������� 66 Defining Multiple Constants with a Single Statement �������������������������������������������������������������������������� 68 Revisiting Literal Values������������������������������������������������������������������������������������������������������������������������ 69 Using Variables ��������������������������������������������������������������������������������������������������������������� 70 Omitting the Variable’s Data Type ��������������������������������������������������������������������������������������������������������� 71 Omitting the Variable’s Value Assignment �������������������������������������������������������������������������������������������� 72 Defining Multiple Variables with a Single Statement ���������������������������������������������������������������������������� 74 Using the Short Variable Declaration Syntax ���������������������������������������������������������������������������������������� 74 Using the Blank Identifier ����������������������������������������������������������������������������������������������� 77 ■ Table of ConTenTs
viii Understanding Pointers �������������������������������������������������������������������������������������������������� 78 Defining a Pointer ��������������������������������������������������������������������������������������������������������������������������������� 80 Following a Pointer ������������������������������������������������������������������������������������������������������������������������������� 81 Understanding Pointer Zero Values ������������������������������������������������������������������������������������������������������ 83 Pointing at Pointers ������������������������������������������������������������������������������������������������������������������������������ 84 Understanding Why Pointers Are Useful ����������������������������������������������������������������������������������������������� 85 Summary ������������������������������������������������������������������������������������������������������������������������ 87 ■Chapter 5: Operations and Conversions �������������������������������������������������������������� 89 Preparing for This Chapter ��������������������������������������������������������������������������������������������� 90 Understanding the Go Operators ������������������������������������������������������������������������������������ 91 Understanding the Arithmetic Operators ���������������������������������������������������������������������������������������������� 91 Concatenating Strings �������������������������������������������������������������������������������������������������������������������������� 95 Understanding the Comparison Operators ������������������������������������������������������������������������������������������� 95 Understanding the Logical Operators ��������������������������������������������������������������������������������������������������� 99 Converting, Parsing, and Formatting Values ����������������������������������������������������������������� 100 Performing Explicit Type Conversions ������������������������������������������������������������������������������������������������ 100 Converting Floating-Point Values to Integers�������������������������������������������������������������������������������������� 102 Parsing from Strings ��������������������������������������������������������������������������������������������������������������������������� 103 Formatting Values as Strings �������������������������������������������������������������������������������������������������������������� 112 Summary ���������������������������������������������������������������������������������������������������������������������� 115 ■Chapter 6: Flow Control ������������������������������������������������������������������������������������� 117 Preparing for This Chapter ������������������������������������������������������������������������������������������� 118 Understanding Flow Control ����������������������������������������������������������������������������������������� 119 Using if Statements ������������������������������������������������������������������������������������������������������ 119 Using the else Keyword ���������������������������������������������������������������������������������������������������������������������� 121 Understanding if Statement Scope ����������������������������������������������������������������������������������������������������� 124 Using an Initialization Statement with an if Statement ���������������������������������������������������������������������� 125 Using for Loops ������������������������������������������������������������������������������������������������������������ 127 Incorporating the Condition into the Loop ������������������������������������������������������������������������������������������ 128 Using Initialization and Completion Statements ��������������������������������������������������������������������������������� 129 ■ Table of ConTenTs
ix Continuing Loops �������������������������������������������������������������������������������������������������������������������������������� 131 Enumerating Sequences ��������������������������������������������������������������������������������������������������������������������� 132 Using switch Statements ��������������������������������������������������������������������������������������������� 135 Matching Multiple Values ������������������������������������������������������������������������������������������������������������������� 137 Forcing Falling Through to the Next case Statement�������������������������������������������������������������������������� 139 Providing a default Clause ������������������������������������������������������������������������������������������������������������������ 139 Using an Initialization Statement �������������������������������������������������������������������������������������������������������� 140 Omitting a Comparison Value ������������������������������������������������������������������������������������������������������������� 142 Using Label Statements ����������������������������������������������������������������������������������������������� 143 Summary ���������������������������������������������������������������������������������������������������������������������� 145 ■Chapter 7: Using Arrays, Slices, and Maps �������������������������������������������������������� 147 Preparing for This Chapter ������������������������������������������������������������������������������������������� 148 Working with Arrays ����������������������������������������������������������������������������������������������������� 149 Using the Array Literal Syntax ������������������������������������������������������������������������������������������������������������ 150 Understanding Array Types ����������������������������������������������������������������������������������������������������������������� 151 Understanding Array Values ���������������������������������������������������������������������������������������������������������������� 152 Comparing Arrays ������������������������������������������������������������������������������������������������������������������������������� 154 Enumerating Arrays ���������������������������������������������������������������������������������������������������������������������������� 154 Working with Slices ������������������������������������������������������������������������������������������������������ 156 Appending Elements to a Slice ����������������������������������������������������������������������������������������������������������� 158 Appending One Slice to Another ��������������������������������������������������������������������������������������������������������� 163 Creating Slices from Existing Arrays �������������������������������������������������������������������������������������������������� 163 Specifying Capacity When Creating a Slice from an Array ����������������������������������������������������������������� 169 Creating Slices from Other Slices ������������������������������������������������������������������������������������������������������� 170 Using the copy Function ��������������������������������������������������������������������������������������������������������������������� 171 Deleting Slice Elements ���������������������������������������������������������������������������������������������������������������������� 175 Enumerating Slices ����������������������������������������������������������������������������������������������������������������������������� 176 Sorting Slices ������������������������������������������������������������������������������������������������������������������������������������� 176 Comparing Slices �������������������������������������������������������������������������������������������������������������������������������� 177 Getting the Array Underlying a Slice ��������������������������������������������������������������������������������������������������� 178 ■ Table of ConTenTs
x Working with Maps ������������������������������������������������������������������������������������������������������ 179 Using the Map Literal Syntax �������������������������������������������������������������������������������������������������������������� 180 Checking for Items in a Map ��������������������������������������������������������������������������������������������������������������� 181 Removing Items from a Map �������������������������������������������������������������������������������������������������������������� 183 Enumerating the Contents of a Map ��������������������������������������������������������������������������������������������������� 184 Understanding the Dual Nature of Strings �������������������������������������������������������������������� 185 Converting a String to Runes �������������������������������������������������������������������������������������������������������������� 189 Enumerating Strings ��������������������������������������������������������������������������������������������������������������������������� 191 Summary ���������������������������������������������������������������������������������������������������������������������� 193 ■Chapter 8: Defining and Using Functions ���������������������������������������������������������� 195 Preparing for This Chapter ������������������������������������������������������������������������������������������� 196 Defining a Simple Function ������������������������������������������������������������������������������������������ 197 Defining and Using Function Parameters ��������������������������������������������������������������������� 199 Omitting Parameter Types ������������������������������������������������������������������������������������������������������������������ 200 Omitting Parameter Names ���������������������������������������������������������������������������������������������������������������� 201 Defining Variadic Parameters ������������������������������������������������������������������������������������������������������������� 202 Using Pointers as Function Parameters ���������������������������������������������������������������������������������������������� 206 Defining and Using Function Results ��������������������������������������������������������������������������� 207 Returning Multiple Function Results �������������������������������������������������������������������������������������������������� 209 Using the defer Keyword ���������������������������������������������������������������������������������������������� 216 Summary ���������������������������������������������������������������������������������������������������������������������� 217 ■Chapter 9: Using Function Types ����������������������������������������������������������������������� 219 Preparing for This Chapter ������������������������������������������������������������������������������������������� 220 Understanding Function Types ������������������������������������������������������������������������������������� 220 Understanding Function Comparisons and the Zero Type ������������������������������������������������������������������ 222 Using Functions as Arguments ����������������������������������������������������������������������������������������������������������� 223 Using Functions as Results����������������������������������������������������������������������������������������������������������������� 225 Creating Function Type Aliases ������������������������������������������������������������������������������������� 226 Using the Literal Function Syntax ��������������������������������������������������������������������������������� 228 Understanding Function Variable Scope ��������������������������������������������������������������������������������������������� 230 ■ Table of ConTenTs
xi Using Functions Values Directly���������������������������������������������������������������������������������������������������������� 231 Understanding Function Closure �������������������������������������������������������������������������������������������������������� 233 Summary ���������������������������������������������������������������������������������������������������������������������� 242 ■Chapter 10: Defining Structs ����������������������������������������������������������������������������� 243 Preparing for This Chapter ������������������������������������������������������������������������������������������� 244 Defining and Using a Struct������������������������������������������������������������������������������������������ 245 Creating Struct Values ������������������������������������������������������������������������������������������������������������������������ 246 Using a Struct Value ��������������������������������������������������������������������������������������������������������������������������� 247 Partially Assigning Struct Values �������������������������������������������������������������������������������������������������������� 248 Using Field Positions to Create Struct Values ������������������������������������������������������������������������������������� 250 Defining Embedded Fields ������������������������������������������������������������������������������������������������������������������ 251 Comparing Struct Values �������������������������������������������������������������������������������������������������������������������� 253 Defining Anonymous Struct Types ������������������������������������������������������������������������������������������������������ 255 Creating Arrays, Slices, and Maps Containing Struct Values ���������������������������������������� 257 Understanding Structs and Pointers ���������������������������������������������������������������������������� 258 Understanding the Struct Pointer Convenience Syntax ���������������������������������������������������������������������� 260 Understanding Pointers to Values ������������������������������������������������������������������������������������������������������� 262 Understanding Struct Constructor Functions ������������������������������������������������������������������������������������� 264 Using Pointer Types for Struct Fields �������������������������������������������������������������������������������������������������� 266 Understanding Zero Value for Structs and Pointers to Structs ����������������������������������������������������������� 270 Summary ���������������������������������������������������������������������������������������������������������������������� 272 ■Chapter 11: Using Methods and Interfaces ������������������������������������������������������� 273 Preparing for This Chapter ������������������������������������������������������������������������������������������� 274 Defining and Using Methods ���������������������������������������������������������������������������������������� 275 Defining Method Parameters and Results ������������������������������������������������������������������������������������������ 278 Understanding Method Overloading ��������������������������������������������������������������������������������������������������� 279 Understanding Pointer and Value Receivers ��������������������������������������������������������������������������������������� 282 Defining Methods for Type Aliases ������������������������������������������������������������������������������������������������������ 285 Putting Types and Methods in Separate Files �������������������������������������������������������������� 287 ■ Table of ConTenTs
xii Defining and Using Interfaces �������������������������������������������������������������������������������������� 288 Defining an Interface �������������������������������������������������������������������������������������������������������������������������� 288 Implementing an Interface ����������������������������������������������������������������������������������������������������������������� 290 Using an Interface ������������������������������������������������������������������������������������������������������������������������������ 291 Understanding the Effect of Pointer Method Receivers ���������������������������������������������������������������������� 294 Comparing Interface Values ����������������������������������������������������������������������������������������� 296 Performing Type Assertions ������������������������������������������������������������������������������������������ 298 Testing Before Performing a Type Assertion ��������������������������������������������������������������������������������������� 299 Switching on Dynamic Types �������������������������������������������������������������������������������������������������������������� 301 Using the Empty Interface �������������������������������������������������������������������������������������������� 302 Using the Empty Interface for Function Parameters ��������������������������������������������������������������������������� 304 Summary ���������������������������������������������������������������������������������������������������������������������� 307 ■Chapter 12: Creating and Using Packages �������������������������������������������������������� 309 Preparing for This Chapter ������������������������������������������������������������������������������������������� 310 Understanding the Module File ������������������������������������������������������������������������������������ 311 Creating a Custom Package ����������������������������������������������������������������������������������������� 311 Using a Custom Package �������������������������������������������������������������������������������������������������������������������� 312 Understanding Package Access Control ��������������������������������������������������������������������������������������������� 314 Adding Code Files to Packages ���������������������������������������������������������������������������������������������������������� 316 Dealing with Package Name Conflicts ������������������������������������������������������������������������������������������������ 318 Creating Nested Packages ������������������������������������������������������������������������������������������������������������������ 321 Using Package Initialization Functions ����������������������������������������������������������������������������������������������� 323 Using External Packages ���������������������������������������������������������������������������������������������� 326 Managing External Packages ������������������������������������������������������������������������������������������������������������� 329 Summary ���������������������������������������������������������������������������������������������������������������������� 330 ■ Table of ConTenTs
xiii ■Chapter 13: Type and Interface Composition ����������������������������������������������������� 331 Preparing for This Chapter ������������������������������������������������������������������������������������������� 332 Understanding Type Composition ��������������������������������������������������������������������������������� 332 Defining the Base Type ����������������������������������������������������������������������������������������������������������������������� 333 Composing Types ��������������������������������������������������������������������������������������������������������� 334 Creating a Chain of Nested Types ������������������������������������������������������������������������������������������������������� 338 Using Multiple Nested Types in the Same Struct �������������������������������������������������������������������������������� 339 Understanding When Promotion Cannot Be Performed ���������������������������������������������������������������������� 340 Understanding Composition and Interfaces ����������������������������������������������������������������� 343 Using Composition to Implement Interfaces ��������������������������������������������������������������������������������������� 344 Composing Interfaces ������������������������������������������������������������������������������������������������������������������������� 348 Summary ���������������������������������������������������������������������������������������������������������������������� 350 ■Chapter 14: Using Goroutines and Channels ����������������������������������������������������� 351 Preparing for This Chapter ������������������������������������������������������������������������������������������� 352 Understanding How Go Executes Code ������������������������������������������������������������������������ 354 Creating Additional Goroutines ������������������������������������������������������������������������������������� 356 Returning Results from Goroutines ������������������������������������������������������������������������������ 360 Sending a Result Using a Channel ������������������������������������������������������������������������������������������������������ 362 Receiving a Result Using a Channel ��������������������������������������������������������������������������������������������������� 363 Working with Channels ������������������������������������������������������������������������������������������������ 366 Coordinating Channels ������������������������������������������������������������������������������������������������������������������������ 366 Sending and Receiving an Unknown Number of Values ��������������������������������������������������������������������� 371 Restricting Channel Direction ������������������������������������������������������������������������������������������������������������� 376 Using Select Statements ���������������������������������������������������������������������������������������������� 381 Receiving Without Blocking ���������������������������������������������������������������������������������������������������������������� 382 Receiving from Multiple Channels ������������������������������������������������������������������������������������������������������ 384 Sending Without Blocking ������������������������������������������������������������������������������������������������������������������� 386 Sending to Multiple Channels ������������������������������������������������������������������������������������������������������������� 388 Summary ���������������������������������������������������������������������������������������������������������������������� 390 ■ Table of ConTenTs
xiv ■Chapter 15: Error Handling �������������������������������������������������������������������������������� 391 Preparing for This Chapter ������������������������������������������������������������������������������������������� 392 Dealing with Recoverable Errors ���������������������������������������������������������������������������������� 394 Generating Errors ������������������������������������������������������������������������������������������������������������������������������� 394 Reporting Errors via Channels ������������������������������������������������������������������������������������������������������������ 396 Using the Error Convenience Functions ���������������������������������������������������������������������������������������������� 398 Dealing with Unrecoverable Errors ������������������������������������������������������������������������������� 401 Recovering from Panics ���������������������������������������������������������������������������������������������������������������������� 402 Panicking After a Recovery ����������������������������������������������������������������������������������������������������������������� 404 Recovering from Panics in Go Routines ���������������������������������������������������������������������������������������������� 406 Summary ���������������������������������������������������������������������������������������������������������������������� 409 ■Part II: Using the Go Standard Library �������������������������������������������� 411 ■Chapter 16: String Processing and Regular Expressions ���������������������������������� 413 Preparing for This Chapter ������������������������������������������������������������������������������������������� 414 Processing Strings ������������������������������������������������������������������������������������������������������� 415 Comparing Strings ������������������������������������������������������������������������������������������������������������������������������ 415 Converting String Case ����������������������������������������������������������������������������������������������������������������������� 417 Working with Character Case ������������������������������������������������������������������������������������������������������������� 419 Inspecting Strings ������������������������������������������������������������������������������������������������������������������������������� 420 Manipulating Strings �������������������������������������������������������������������������������������������������������������������������� 422 Trimming Strings �������������������������������������������������������������������������������������������������������������������������������� 427 Altering Strings ����������������������������������������������������������������������������������������������������������������������������������� 430 Building and Generating Strings ��������������������������������������������������������������������������������������������������������� 433 Using Regular Expressions ������������������������������������������������������������������������������������������� 435 Compiling and Reusing Patterns �������������������������������������������������������������������������������������������������������� 436 Splitting Strings Using a Regular Expression ������������������������������������������������������������������������������������� 439 Using Subexpressions ������������������������������������������������������������������������������������������������������������������������ 440 Replacing Substrings Using a Regular Expression ����������������������������������������������������������������������������� 443 Summary ���������������������������������������������������������������������������������������������������������������������� 445 ■ Table of ConTenTs
xv ■Chapter 17: Formatting and Scanning Strings �������������������������������������������������� 447 Preparing for This Chapter ������������������������������������������������������������������������������������������� 448 Writing Strings ������������������������������������������������������������������������������������������������������������� 449 Formatting Strings ������������������������������������������������������������������������������������������������������� 450 Understanding the Formatting Verbs �������������������������������������������������������������������������������������������������� 452 Using the General-Purpose Formatting Verbs ������������������������������������������������������������������������������������ 452 Using the Integer Formatting Verbs ���������������������������������������������������������������������������������������������������� 455 Using the Floating-Point Formatting Verbs ����������������������������������������������������������������������������������������� 456 Using the String and Character Formatting Verbs ������������������������������������������������������������������������������ 459 Using the Boolean Formatting Verb ���������������������������������������������������������������������������������������������������� 460 Using the Pointer Formatting Verb ������������������������������������������������������������������������������������������������������ 460 Scanning Strings ���������������������������������������������������������������������������������������������������������� 461 Dealing with Newline Characters ������������������������������������������������������������������������������������������������������� 464 Using a Different String Source ���������������������������������������������������������������������������������������������������������� 465 Using a Scanning Template ���������������������������������������������������������������������������������������������������������������� 466 Summary ���������������������������������������������������������������������������������������������������������������������� 466 ■Chapter 18: Math Functions and Data Sorting �������������������������������������������������� 467 Preparing for This Chapter ������������������������������������������������������������������������������������������� 468 Working with Numbers ������������������������������������������������������������������������������������������������� 469 Generating Random Numbers ������������������������������������������������������������������������������������������������������������ 471 Sorting Data ����������������������������������������������������������������������������������������������������������������� 475 Sorting Number and String Slices ������������������������������������������������������������������������������������������������������ 475 Searching Sorted Data ������������������������������������������������������������������������������������������������������������������������ 477 Sorting Custom Data Types ����������������������������������������������������������������������������������������������������������������� 479 Summary ���������������������������������������������������������������������������������������������������������������������� 485 ■Chapter 19: Dates, Times, and Durations ���������������������������������������������������������� 487 Preparing for This Chapter ������������������������������������������������������������������������������������������� 488 Working with Dates and Times ������������������������������������������������������������������������������������� 489 Representing Dates and Times ����������������������������������������������������������������������������������������������������������� 489 Representing Durations ���������������������������������������������������������������������������������������������������������������������� 501 ■ Table of ConTenTs
xvi Using the Time Features for Goroutines and Channels ������������������������������������������������ 505 Putting a Goroutine to Sleep ��������������������������������������������������������������������������������������������������������������� 505 Deferring Execution of a Function ������������������������������������������������������������������������������������������������������ 506 Receiving Timed Notifications ������������������������������������������������������������������������������������������������������������ 507 Receiving Recurring Notifications ������������������������������������������������������������������������������������������������������ 512 Summary ���������������������������������������������������������������������������������������������������������������������� 514 ■Chapter 20: Reading and Writing Data �������������������������������������������������������������� 515 Preparing for This Chapter ������������������������������������������������������������������������������������������� 516 Understanding Readers and Writers ����������������������������������������������������������������������������� 518 Understanding Readers ���������������������������������������������������������������������������������������������������������������������� 518 Understanding Writers ������������������������������������������������������������������������������������������������������������������������ 519 Using the Utility Functions for Readers and Writers ����������������������������������������������������� 521 Using the Specialized Readers and Writers������������������������������������������������������������������ 522 Using Pipes ����������������������������������������������������������������������������������������������������������������������������������������� 522 Concatenating Multiple Readers ��������������������������������������������������������������������������������������������������������� 526 Combining Multiple Writers ���������������������������������������������������������������������������������������������������������������� 527 Echoing Reads to a Writer ������������������������������������������������������������������������������������������������������������������ 528 Limiting Read Data ����������������������������������������������������������������������������������������������������������������������������� 529 Buffering Data �������������������������������������������������������������������������������������������������������������� 529 Using the Additional Buffered Reader Methods ���������������������������������������������������������������������������������� 533 Performing Buffered Writes ���������������������������������������������������������������������������������������������������������������� 534 Formatting and Scanning with Readers and Writers ���������������������������������������������������� 537 Scanning Values from a Reader ���������������������������������������������������������������������������������������������������������� 537 Writing Formatted Strings to a Writer ������������������������������������������������������������������������������������������������� 539 Using a Replacer with a Writer ����������������������������������������������������������������������������������������������������������� 540 Summary ���������������������������������������������������������������������������������������������������������������������� 541 ■ Table of ConTenTs
xvii ■Chapter 21: Working with JSON Data ���������������������������������������������������������������� 543 Preparing for This Chapter ������������������������������������������������������������������������������������������� 544 Reading and Writing JSON Data ����������������������������������������������������������������������������������� 544 Encoding JSON Data ��������������������������������������������������������������������������������������������������������������������������� 545 Decoding JSON Data��������������������������������������������������������������������������������������������������������������������������� 555 Summary ���������������������������������������������������������������������������������������������������������������������� 568 ■Chapter 22: Working with Files ������������������������������������������������������������������������� 569 Preparing for This Chapter ������������������������������������������������������������������������������������������� 570 Reading Files ���������������������������������������������������������������������������������������������������������������� 571 Using the Read Convenience Function ����������������������������������������������������������������������������������������������� 572 Using the File Struct to Read a File ���������������������������������������������������������������������������������������������������� 574 Writing to Files ������������������������������������������������������������������������������������������������������������� 578 Using the Write Convenience Function ����������������������������������������������������������������������������������������������� 578 Using the File Struct to Write to a File ������������������������������������������������������������������������������������������������ 579 Writing JSON Data to a File ���������������������������������������������������������������������������������������������������������������� 581 Using the Convenience Functions to Create New Files ������������������������������������������������ 582 Working with File Paths������������������������������������������������������������������������������������������������ 584 Managing Files and Directories ������������������������������������������������������������������������������������ 586 Exploring the File System ��������������������������������������������������������������������������������������������� 588 Determining Whether a File Exists ������������������������������������������������������������������������������������������������������ 590 Locating Files Using a Pattern ������������������������������������������������������������������������������������������������������������ 591 Processing All Files in a Directory ������������������������������������������������������������������������������������������������������ 592 Summary ���������������������������������������������������������������������������������������������������������������������� 593 ■Chapter 23: Using HTML and Text Templates ����������������������������������������������������� 595 Preparing for This Chapter ������������������������������������������������������������������������������������������� 596 Creating HTML Templates��������������������������������������������������������������������������������������������� 598 Loading and Executing Templates ������������������������������������������������������������������������������������������������������ 599 Understanding Template Actions �������������������������������������������������������������������������������������������������������� 603 Creating Text Templates ����������������������������������������������������������������������������������������������� 623 Summary ���������������������������������������������������������������������������������������������������������������������� 625 ■ Table of ConTenTs
xviii ■Chapter 24: Creating HTTP Servers ������������������������������������������������������������������� 627 Preparing for This Chapter ������������������������������������������������������������������������������������������� 628 Creating a Simple HTTP Server ������������������������������������������������������������������������������������ 630 Creating the HTTP Listener and Handler ��������������������������������������������������������������������������������������������� 631 Inspecting the Request ����������������������������������������������������������������������������������������������������������������������� 632 Filtering Requests and Generating Responses ����������������������������������������������������������������������������������� 635 Using the Response Convenience Functions �������������������������������������������������������������������������������������� 637 Using the Convenience Routing Handler �������������������������������������������������������������������������������������������� 638 Supporting HTTPS Requests ��������������������������������������������������������������������������������������������������������������� 640 Creating a Static HTTP Server �������������������������������������������������������������������������������������� 643 Creating the Static File Route ������������������������������������������������������������������������������������������������������������� 644 Using Templates to Generate Responses ��������������������������������������������������������������������� 646 Responding with JSON Data ���������������������������������������������������������������������������������������� 650 Handling Form Data ����������������������������������������������������������������������������������������������������� 651 Reading Form Data from Requests ����������������������������������������������������������������������������������������������������� 652 Reading Multipart Forms �������������������������������������������������������������������������������������������������������������������� 655 Reading and Setting Cookies ��������������������������������������������������������������������������������������� 659 Summary ���������������������������������������������������������������������������������������������������������������������� 661 ■Chapter 25: Creating HTTP Clients ��������������������������������������������������������������������� 663 Preparing for This Chapter ������������������������������������������������������������������������������������������� 664 Sending Simple HTTP Requests ����������������������������������������������������������������������������������� 668 Sending POST Requests ��������������������������������������������������������������������������������������������������������������������� 673 Configuring HTTP Client Requests �������������������������������������������������������������������������������� 675 Using the Convenience Functions to Create a Request ���������������������������������������������������������������������� 678 Working with Cookies ������������������������������������������������������������������������������������������������������������������������� 679 Managing Redirections ����������������������������������������������������������������������������������������������������������������������� 684 Creating Multipart Forms ��������������������������������������������������������������������������������������������� 687 Summary ���������������������������������������������������������������������������������������������������������������������� 691 ■ Table of ConTenTs
xix ■Chapter 26: Working with Databases ���������������������������������������������������������������� 693 Preparing for This Chapter ������������������������������������������������������������������������������������������� 694 Preparing the Database ���������������������������������������������������������������������������������������������������������������������� 695 Installing a Database Driver ����������������������������������������������������������������������������������������� 697 Opening a Database ����������������������������������������������������������������������������������������������������� 697 Executing Statements and Queries ������������������������������������������������������������������������������ 699 Querying for Multiple Rows ���������������������������������������������������������������������������������������������������������������� 700 Executing Statements with Placeholders ������������������������������������������������������������������������������������������� 706 Executing Queries for Single Rows ����������������������������������������������������������������������������������������������������� 708 Executing Other Queries ��������������������������������������������������������������������������������������������������������������������� 709 Using Prepared Statements ������������������������������������������������������������������������������������������ 711 Using Transactions ������������������������������������������������������������������������������������������������������� 714 Using Reflection to Scan Data into a Struct ����������������������������������������������������������������� 716 Summary ���������������������������������������������������������������������������������������������������������������������� 721 ■Chapter 27: Using Reflection ����������������������������������������������������������������������������� 723 Preparing for This Chapter ������������������������������������������������������������������������������������������� 724 Understanding the Need for Reflection ������������������������������������������������������������������������ 726 Using Reflection ����������������������������������������������������������������������������������������������������������� 727 Using the Basic Type Features ������������������������������������������������������������������������������������������������������������ 731 Using the Basic Value Features ���������������������������������������������������������������������������������������������������������� 733 Identifying Types ���������������������������������������������������������������������������������������������������������� 736 Identifying Byte Slices ������������������������������������������������������������������������������������������������������������������������ 738 Obtaining Underlying Values����������������������������������������������������������������������������������������� 739 Setting a Value Using Reflection ���������������������������������������������������������������������������������� 741 Setting One Value Using Another �������������������������������������������������������������������������������������������������������� 744 Comparing Values ��������������������������������������������������������������������������������������������������������� 745 Using the Comparison Convenience Function ������������������������������������������������������������������������������������ 747 ■ Table of ConTenTs
xx Converting Values ��������������������������������������������������������������������������������������������������������� 748 Converting Numeric Types ������������������������������������������������������������������������������������������������������������������ 749 Creating New Values ���������������������������������������������������������������������������������������������������� 751 Summary ���������������������������������������������������������������������������������������������������������������������� 753 ■Chapter 28: Using Reflection, Part 2 ������������������������������������������������������������������ 755 Preparing for This Chapter ������������������������������������������������������������������������������������������� 755 Working with Pointers �������������������������������������������������������������������������������������������������� 756 Working with Pointer Values ��������������������������������������������������������������������������������������������������������������� 757 Working with Array and Slice Types ����������������������������������������������������������������������������� 759 Working with Array and Slice Values ���������������������������������������������������������������������������� 760 Enumerating Slices and Arrays ����������������������������������������������������������������������������������������������������������� 762 Creating New Slices from Existing Slices ������������������������������������������������������������������������������������������� 764 Creating, Copying, and Appending Elements to Slices������������������������������������������������������������������������ 765 Working with Map Types ���������������������������������������������������������������������������������������������� 766 Working with Map Values ��������������������������������������������������������������������������������������������� 767 Setting and Removing Map Values ����������������������������������������������������������������������������������������������������� 770 Creating New Maps ���������������������������������������������������������������������������������������������������������������������������� 771 Working with Struct Types �������������������������������������������������������������������������������������������� 772 Processing Nested Fields ������������������������������������������������������������������������������������������������������������������� 775 Locating a Field by Name ������������������������������������������������������������������������������������������������������������������� 777 Inspecting Struct Tags ������������������������������������������������������������������������������������������������������������������������ 778 Creating Struct Types �������������������������������������������������������������������������������������������������������������������������� 779 Working with Struct Values ������������������������������������������������������������������������������������������ 780 Setting Struct Field Values������������������������������������������������������������������������������������������������������������������ 782 Summary ���������������������������������������������������������������������������������������������������������������������� 784 ■Chapter 29: Using Reflection, Part 3 ������������������������������������������������������������������ 785 Preparing for This Chapter ������������������������������������������������������������������������������������������� 785 Working with Function Types ��������������������������������������������������������������������������������������� 787 Working with Function Values �������������������������������������������������������������������������������������� 788 Creating and Invoking New Function Types and Values���������������������������������������������������������������������� 791 ■ Table of ConTenTs