Author(s): Delio D’Anna, Andrew Hayes, Sam Hennessy, Jeremy Leasor, Gobin Sougrakpam
Series: Packt
Publisher: Packt Publishing
Year: 2019
Language: English
Pages: 821
Cover......Page 1
FM......Page 2
Copyright......Page 3
Table of Contents......Page 4
Preface......Page 22
Chapter 1: Variables and Operators......Page 34
Introduction......Page 35
What Does Go Look Like?......Page 36
Exercise 1.01: Using Variables, Packages, and Functions to Print Stars......Page 42
Activity 1.01 Defining and Printing......Page 43
Exercise 1.02: Declaring a Variable Using var......Page 44
Declaring Multiple Variables at Once with var......Page 45
Exercise 1.03: Declaring Multiple Variables at Once with var......Page 46
Exercise 1.04: Skipping the Type or Value When Declaring Variables......Page 47
Short Variable Declaration......Page 49
Exercise 1.05: Implementing Short Variable Declaration......Page 50
Declaring Multiple Variables with a Short Variable Declaration......Page 51
Exercise 1.06: Declaring Multiple Variables from a Function......Page 52
Using var to Declare Multiple Variables in One Line......Page 53
Non-English Variable Names......Page 54
Exercise 1.07: Changing the Value of a Variable......Page 55
Changing Multiple Values at Once......Page 56
Exercise 1.08: Changing Multiple Values at Once......Page 57
Exercise 1.09 Using Operators with Numbers......Page 58
Shorthand Operator......Page 61
Exercise 1.10: Implementing Shorthand Operators......Page 62
Comparing Values......Page 63
Exercise 1.11: Comparing Values......Page 64
Zero Values......Page 65
Exercise 1.12 Zero Values......Page 66
Value versus Pointer......Page 68
Exercise 1.13: Getting a Pointer......Page 70
Exercise 1.14: Getting a Value from a Pointer......Page 72
Exercise 1.15: Function Design with Pointers......Page 74
Activity 1.02: Pointer Value Swap......Page 76
Constants......Page 77
Exercise 1.16: Constants......Page 78
Enums......Page 80
Scope......Page 82
Activity 1.03: Message Bug......Page 84
Activity 1.04: Bad Count Bug......Page 85
Summary......Page 86
Chapter 2: Logic and Loops......Page 88
if Statements......Page 89
Exercise 2.01: A Simple if Statement......Page 90
if else Statements......Page 91
Exercise 2.02: Using an if else Statement......Page 92
Exercise 2.03: Using an else if Statement......Page 93
The Initial if Statement......Page 94
Exercise 2.04: Implementing the Initial if Statements......Page 95
Activity 2.01: Implementing FizzBuzz......Page 97
Expression switch Statements......Page 99
Exercise 2.05: Using a switch Statement......Page 100
Exercise 2.06: switch Statements and Multiple case Values......Page 101
Exercise 2.07: Expressionless switch Statements......Page 103
Loops......Page 104
Exercise 2.08: Using the for i Loop......Page 105
Exercise 2.09: Looping Over Arrays and Slices......Page 106
The range Loop......Page 107
Exercise 2.10: Looping Over a Map......Page 108
Activity 2.02: Looping Over Map Data Using range......Page 109
break and continue......Page 110
Exercise 2.11: Using break and continue to Control Loops......Page 111
Activity 2.03: Bubble Sort......Page 113
Summary......Page 114
Chapter 3: Core Types......Page 116
Introduction......Page 117
Exercise 3.01: Program to Measure Password Complexity......Page 118
Numbers......Page 121
Integer......Page 122
Exercise 3.02: Floating-Point Number Accuracy......Page 124
Overflow and Wraparound......Page 126
Exercise 3.03: Triggering Number Wraparound......Page 127
Exercise 3.04: Big Numbers......Page 128
Text......Page 130
Rune......Page 132
Exercise 3.05: Safely Looping over a String......Page 134
Activity 3.01: Sales Tax Calculator......Page 137
Activity 3.02: Loan Calculator......Page 138
Summary......Page 140
Chapter 4: Complex Types......Page 142
Collection Types......Page 143
Exercise 4.01: Defining an Array......Page 144
Exercise 4.02: Comparing Arrays......Page 145
Exercise 4.03: Initializing an Array Using Keys......Page 147
Reading from an Array......Page 148
Writing to an Array......Page 149
Exercise 4.05: Writing to an Array......Page 150
Exercise 4.06: Looping Over an Array Using a "for i" Loop......Page 151
Exercise 4.07: Modifying the Contents of an Array in a Loop......Page 153
Activity 4.01: Filling an Array......Page 154
Slice......Page 155
Exercise 4.08: Working with Slices......Page 156
Appending Multiple Items to a Slice......Page 158
Exercise 4.09: Appending Multiple Items to a Slice......Page 159
Activity 4.03: Creating a Locale Checker......Page 160
Creating Slices from Slices and Arrays......Page 161
Exercise 4.10: Creating Slices from a Slice......Page 162
Understanding Slice Internals......Page 163
Exercise 4.11: Using make to Control the Capacity of a Slice......Page 164
Exercise 4.12: Controlling Internal Slice Behavior......Page 166
Map Fundamentals......Page 171
Exercise 4.13: Creating, Reading, and Writing a Map......Page 172
Reading from Maps......Page 173
Exercise 4.14: Reading from a Map......Page 174
Activity 4.04: Slicing the Week......Page 176
Exercise 4.15: Deleting an Element from a Map......Page 177
Simple Custom Types......Page 179
Exercise 4.16: Creating a Simple Custom Type......Page 180
Structs......Page 181
Exercise 4.17: Creating Struct Types and Values......Page 182
Exercise 4.18: Comparing Structs to Each Other......Page 185
Struct Composition Using Embedding......Page 187
Exercise 4.19: Struct Embedding and Initialization......Page 188
Exercise 4.20: Numeric Type Conversion......Page 191
Type Assertions and interface{}......Page 193
Exercise 4.21: Type Assertion......Page 194
Exercise 4.22: Type Switch......Page 196
Activity 4.06: Type Checker......Page 199
Summary......Page 200
Chapter 5: Functions......Page 202
Functions......Page 203
Parts of a function......Page 204
fizzBuzz......Page 207
Exercise 5.01: Creating a Function to Print Salesperson Expectation Ratings from the Number of Items Sold......Page 210
Parameters......Page 212
The Difference between an Argument and a Parameter......Page 214
Exercise 5.02: Mapping Index Values to Column Headers......Page 215
Function Variable Scope......Page 216
Exercise 5.03: Creating a fizzBuzz Function with Return Values......Page 218
Activity 5.01: Calculating the Working Hours of Employees......Page 220
Naked Returns......Page 221
Exercise 5.04: Mapping a CSV Index to a Column Header with Return Values......Page 224
Variadic Function......Page 225
Exercise 5.05: Summing Numbers......Page 229
Anonymous Functions......Page 230
Exercise 5.06: Creating an Anonymous Function to Calculate the Square Root of a Number......Page 232
Closures......Page 233
Exercise 5.07: Creating a Closure Function to Decrement a Counter......Page 235
Function Types......Page 236
Exercise 5.08: Creating Various Functions to Calculate Salary......Page 240
defer......Page 242
Activity 5.02: Calculating Payable Amount for Employees Based on Working Hours......Page 245
Summary......Page 247
Chapter 6: Errors......Page 250
What Are Errors?......Page 251
Syntax Errors......Page 252
Exercise 6.01: Runtime Errors While Adding Numbers......Page 253
Exercise 6.02: Logic Error with Walking Distance......Page 255
Error Handling Using Other Programming Languages......Page 257
Error Interface Type......Page 258
Creating Error Values......Page 261
Exercise 6.03: Creating an Application to Calculate Pay for the Week......Page 262
Panic......Page 265
Exercise 6.04: Crashing the Program on Errors Using panic......Page 270
Recover......Page 273
Exercise 6.05: Recovering from a Panic......Page 275
Activity 6.01: Creating a Custom Error Message for a Banking Application......Page 278
Activity 6.02: Validating a Bank Customer's Direct Deposit Submission......Page 279
Activity 6.03: Panic on Invalid Data Submission......Page 280
Activity 6.04: Preventing a Panic from Crashing the App......Page 281
Summary......Page 282
Chapter 7: Interfaces......Page 284
Introduction......Page 285
Interface......Page 286
Defining an Interface......Page 288
Implementing an Interface......Page 289
Advantages of Implementing Interfaces Implicitly......Page 291
Exercise 7.01: Implementing an Interface......Page 292
Duck Typing......Page 294
Polymorphism......Page 296
Exercise 7.02: Calculating the Area of Different Shapes Using Polymorphism......Page 302
Accepting Interfaces and Returning Structs......Page 305
Empty interface{}......Page 308
Type Assertion and Switches......Page 310
Exercise 7.03: Analyzing Empty interface{} Data......Page 315
Activity 7.01: Calculating Pay and Performance Review......Page 319
Summary......Page 320
Chapter 8: Packages......Page 324
Introduction......Page 325
Reusable......Page 327
Modular......Page 328
What Is a Package?......Page 329
Package Structure......Page 330
Package Naming......Page 331
Package Declarations......Page 333
Exported and Unexported Code......Page 334
GOROOT and GOPATH......Page 336
Package Alias......Page 339
Main Package......Page 340
Exercise 8.01: Creating a Package to Calculate Areas of Various Shapes......Page 341
The init() Function......Page 344
Exercise 8.02: Loading Budget Categories......Page 347
Executing Multiple init() Functions......Page 348
Exercise 8.03: Assigning Payees to Budget Categories......Page 350
Activity 8.01: Creating a Function to Calculate Payroll and Performance Review......Page 351
Summary......Page 352
Chapter 9: Basic Debugging......Page 356
Introduction......Page 357
Methods for Bug-Free Code......Page 358
Performing Logging......Page 359
Exercise 9.01: Working with fmt.Println......Page 360
Formatting Using fmt.Printf()......Page 361
Additional Options for Formatting......Page 365
Exercise 9.02: Printing Decimal, Binary, and Hex Values......Page 368
Basic Debugging......Page 370
Printing Go Variable Types......Page 374
Exercise 9.03 Printing the Go Representation of a Variable......Page 375
Logging......Page 377
Log Fatal Errors......Page 380
Activity 9.01: Building a Program to Validate Social Security Numbers......Page 381
Summary......Page 383
Chapter 10: About Time......Page 386
Making Time......Page 387
Exercise 10.1: Creating a Function to Return a timestamp......Page 390
Comparing Time......Page 391
Duration Calculation......Page 393
Managing Time......Page 396
Exercise 10.2: Duration of Execution......Page 397
Formatting Time......Page 398
Exercise 10.03: What Is the Time in Your Zone?......Page 401
Activity 10.01: Formatting a Date According to User Requirements......Page 402
Activity 10.02: Enforcing a Specific Format of Date and Time......Page 403
Activity 10.03: Measuring Elapsed Time......Page 404
Activity 10.05: Printing the Local Time in Different Time Zones......Page 405
Summary......Page 406
Chapter 11: Encoding and Decoding (JSON)......Page 408
JSON......Page 409
Decoding JSON......Page 413
Struct Tags......Page 415
Exercise 11.01: Unmarshaling Student Courses......Page 421
Encoding JSON......Page 424
Exercise 11.02: Marshaling Student Courses......Page 433
Unknown JSON Structures......Page 437
Exercise 11.03: Analyzing College Class JSON......Page 439
GOB: Go's Own Encoding......Page 442
Exercise 11.04: Using gob to Encode Data......Page 444
Activity 11.01: Mimicking a Customer Order Using JSON......Page 448
Summary......Page 451
Chapter 12: Files and Systems......Page 454
Filesystem......Page 455
File Permissions......Page 456
Flags and Arguments......Page 459
Signals......Page 464
Exercise 12.01: Simulating Cleanup......Page 468
Creating and Writing to Files......Page 471
Reading the Whole File at Once......Page 476
Exercise 12.02: Backing Up Files......Page 483
CSV......Page 488
Activity 12.01: Parsing Bank Transaction Files......Page 492
Summary......Page 495
Chapter 13: SQL and Databases......Page 498
The Database......Page 499
Database API and Drivers......Page 501
Connecting to Databases......Page 502
Creating Tables......Page 505
Inserting Data......Page 508
Exercise 13.01: Creating a Table with Numbers......Page 510
Retrieving Data......Page 512
Updating Existing Data......Page 515
Deleting Data......Page 517
Exercise 13.02: Holding Prime Numbers in a Database......Page 518
Truncating and Deleting Table......Page 521
Activity 13.01: Holding User Data in a Table......Page 522
Activity 13.02: Finding Messages of Specific Users......Page 523
Summary......Page 524
Chapter 14: Using the Go HTTP Client......Page 526
The Go HTTP Client and Its Uses......Page 527
Sending a Request to a Server......Page 528
Exercise 14.01: Sending a Get Request to a Web Server Using the Go HTTP Client......Page 529
Structured Data......Page 531
Exercise 14.02: Using the HTTP Client with Structured Data......Page 532
Activity 14.01: Requesting Data from a Web Server and Processing the Response......Page 534
Sending Data to a Server......Page 535
Exercise 14.03: Sending a Post Request to a Web Server Using the Go HTTP Client......Page 536
Uploading Files in a Post Request......Page 538
Exercise 14.04: Uploading a File to a Web Server via a Post Request......Page 539
Exercise 14.05: Using Custom Headers and Options with the Go HTTP Client......Page 542
Activity 14.02: Sending Data to a Web Server and Checking Whether the Data Was Received Using POST and GET......Page 545
Summary......Page 546
Chapter 15: HTTP Servers......Page 548
How to Build a Basic Server......Page 549
HTTP Handler......Page 550
Exercise 15.01: Creating a Hello World Server......Page 551
Exercise 15.02: Routing Our Server......Page 553
Handler versus Handler Function......Page 556
Activity 15.01: Adding a Page Counter to an HTML Page......Page 557
Activity 15.02: Serving a Request with a JSON Payload......Page 560
Dynamic Content......Page 562
Exercise 15.03: Personalized Welcome......Page 563
Templating......Page 565
Exercise 15.04: Templating Our Pages......Page 567
Static Resources......Page 571
Exercise 15.05: Creating a Hello World Server Using a Static File......Page 572
Getting Some Style......Page 574
Exercise 15.06: A Stylish Welcome......Page 575
Getting Dynamic......Page 579
Activity 15.03: External Template......Page 580
HTTP Methods......Page 582
Exercise 15.07: Completing a Questionnaire......Page 583
JSON loads......Page 587
Exercise 15.08: Building a Server That Accepts JSON Requests......Page 588
Summary......Page 590
Chapter 16: Concurrent Work......Page 592
Goroutines......Page 593
Exercise 16.01: Using Concurrent Routines......Page 594
WaitGroup......Page 597
Exercise 16.02: Experimenting with WaitGroup......Page 598
Race Conditions......Page 600
Atomic Operations......Page 601
Exercise 16.03: An Atomic Change......Page 602
Invisible Concurrency......Page 607
Activity 16.01: Listing Numbers......Page 608
Channels......Page 609
Exercise 16.04: Exchange Greeting Messages via Channels......Page 611
Exercise 16.05: Two-Way Message Exchange with Channels......Page 613
Exercise 16.06: Sum Numbers from Everywhere......Page 614
Exercise 16.07: Request to Goroutines......Page 616
The Importance of Concurrency......Page 618
Exercise 16.08: Equally Splitting the Work between Routines......Page 619
Activity 16.02: Source Files......Page 622
Buffers......Page 623
Exercise 16.09: Notifying When Computation Has Finished......Page 626
HTTP Servers......Page 628
Exercise 16.10: A Structured Work......Page 629
Go Context Package......Page 632
Exercise 16.11: Managing Routines with Context......Page 633
Summary......Page 635
Chapter 17: Using Go Tools......Page 638
Exercise 17.01: Using the go build Tool......Page 639
The go run Tool......Page 640
The gofmt Tool......Page 641
Exercise 17.03: Using the gofmt Tool......Page 642
The goimports Tool......Page 643
Exercise 17.04: Using the goimports Tool......Page 644
The go vet Tool......Page 645
Exercise 17.05: Using the go vet Tool......Page 646
The Go Race Detector......Page 647
Exercise 17.06: Using the Go Race Detector......Page 648
The go doc Tool......Page 650
Exercise 17.07: Implementing the go doc Tool......Page 651
The go get Tool......Page 652
Exercise 17.08: Implementing the go get Tool......Page 653
Activity 17.01: Using gofmt, goimport, go vet, and go get to Correct a File......Page 654
Summary......Page 656
Chapter 18: Security......Page 658
SQL Injection......Page 659
Command Injection......Page 661
Exercise 18.01: Handling SQL Injection......Page 663
Cross-Site Scripting......Page 667
Exercise 18.02: Handling XSS Attacks......Page 668
Hashing Libraries......Page 671
Exercise 18.03: Using Different Hashing Libraries......Page 672
Encryption......Page 674
Exercise 18.04: Symmetric Encryption and Decryption......Page 675
Asymmetric Encryption......Page 677
Exercise 18.05: Asymmetric Encryption and Decryption......Page 678
Random Generators......Page 679
Exercise 18.06: Random Generators......Page 680
Exercise 18.07: Generating a Certificate and Private Key......Page 682
Exercise 18.08: Running an HTTPS Server......Page 686
Password Management......Page 689
Activity 18.01: Authenticating Users on the Application Using Hashed Passwords......Page 690
Activity 18.02: Creating CA Signed Certificates Using Crypto Libraries......Page 691
Summary......Page 692
Chapter 19: Special Features......Page 694
Build Constraints......Page 695
Build Tags......Page 696
Filenames......Page 699
Reflection......Page 700
TypeOf and ValueOf......Page 702
Exercise 19.01: Using Reflection......Page 703
Activity 19.01: Defining Build Constraints Using Filenames......Page 705
DeepEqual......Page 706
Wildcard Pattern......Page 707
The unsafe Package......Page 708
Exercise 19.02: Using cgo with unsafe......Page 710
Activity 19.02: Using Wildcard with Go Test......Page 711
Summary......Page 712
Appendix......Page 716
Index......Page 818