Cover....1
Half Title....2
Title Page....4
Copyright Page....5
Contents....6
Acknowledgments....20
About the Author....22
CHAPTER 1: Crash Course in GoLang....24
IS IT REFERRED TO AS Go OR GoLang?....25
WHAT IS THE PURPOSE OF GoLang?....25
WHY IS Go SO POPULAR?....25
Other Programming Languages vs. Go....26
Python vs. Go....26
But How Does Go Stack Up against Python?....26
SHOULD WE STUDY Go?....27
Go PROGRAMMING FEATURES....27
WHAT MAKES GoLang SUPERIOR TO OTHER COMPUTER LANGUAGES?....28
GoLang’s Core Capability....28
Multithreading and Concurrency....29
From within, Go Empowers Hardware....29
Unmatched Simplicity of Go....30
Built-in Testing and Profiling Framework....30
Easy Learning Curve....30
BEGINNING WITH Go....31
Text Editor....31
Finding a Go Compiler....31
INSTALL Go ON WINDOWS....31
How Can We Know Which Go Language Version Is Preinstalled?....32
Downloading and Installing Go....32
WRITING THE FIRST Go PROGRAM....33
Why Is There a “Go Language”?....34
What Is Missing in Go That the Other Languages Have?....34
Hardware Restrictions....35
The Upsides and Downsides of the Go Language....35
TERMINAL....36
The Open Terminal Tool Window....36
Start the New Session....37
INSTALL Go ON Mac....37
Making Our First Program....39
Implement a Go Program....39
Do Go Programs Interact with Programs Written in C/C++?....40
HOW CAN WE CREATE AN EMPTY FILE IN GoLang?....40
HOW TO MAKE A DIRECTORY IN Go....42
HOW TO READ AND WRITE A Go Program....43
HOW DO WE RENAME AND TRANSFER A FILE IN GoLang?....46
HOW TO READ A FILE LINE BY LINE AND CONVERT IT TO A STRING....47
BASIC SYNTAX....48
Tokens....48
Line Separator....48
Comments....48
Identifiers....49
Keywords....49
Whitespace....50
DATA TYPES IN Go....50
Numbers....51
Booleans....53
Strings....53
VARIABLES IN Go....54
Declaring a Variable....55
Employing the var Keyword....55
Using the Short Variable Declaration....58
CONSTANTS....61
How Should We Declare?....62
Untyped and Typed Numeric Constants....62
Numeric Constant....62
String Literals....64
Boolean Constant....65
VARIABLE SCOPE IN Go....66
Local Variables....66
Global Variables....67
DECLARATION OF MULTIPLE VARIABLES....69
SHORTHAND DECLARATION....71
OPERATORS IN Go....72
Arithmetic Operators....73
Relational Operators....74
Logical Operators....76
Bitwise Operators....76
Assignment Operators....77
Misc Operators....79
CONTROL STATEMENTS....80
if Statement....80
if…else Statement....81
Nested if Statement....83
if..else..if Ladder....84
SWITCH STATEMENT....86
Expression Switch....86
Type Switch....87
ARRAYS....88
Creating and Using an Array....88
Using var Keyword....88
Using a Shorthand Declaration....89
Multidimensional Array....90
How Do You Copy an Array into Another Array?....91
How Can We Pass an Array to a Function?....92
SLICES....93
Slice Declaration....93
Slice Components....94
How Do We Create and Begin a Slice?....95
Using Slice Literal....95
Using an Array....95
Using an Existing Slice....96
Using the make() Function....97
How to Iterate over a Slice....98
Using the for Loop....98
Using Range in a for Loop....99
Using a Blank Identifier in a for Loop....99
Slice Composite Literal....100
How to Sort a Slice of ints....100
Ints....101
IntsAreSorted....101
How Do We Trim a Slice of Bytes in GoLang?....102
How Do We Split a Slice of Bytes?....104
STRING....105
String Literals....106
Using Double Quotes (“”)....106
Using Backticks(“)....106
How Do We Trim a String?....107
Trim....107
TrimLeft....108
TrimRight....109
TrimSpace....110
TrimSuffix....111
TrimPrefix....112
How to Split a String....113
Split....113
SplitAfter....114
SplitAfterN....115
MAPS....116
How Do We Create and Initialize Maps?....117
Simple....117
Using the make() Function....118
WHAT IS A BLANK IDENTIFIER (underscore)?....119
DEFER KEYWORD....120
PANIC IN GoLang....121
RECOVER....122
CLOSURES....123
RECURSION....124
POINTERS....125
What Are Points Used For?....125
Pointers Declaration and Initialization....126
Declaring a Pointer....126
Pointer Initialization....126
Dereferencing a Pointer....127
In GoLang, How Can We Instantiate a Struct Using the New Keyword?....128
Pointers to a Function....129
Create a Pointer and Pass It to the Function....129
Passing an Address of the Variable to Function Call....130
Pointer to a Struct....131
Pointer to Pointer in Go....132
How to Declare a Pointer to a Pointer....132
Comparing Pointers....133
== operator....133
!= operator....134
GoLang STRUCTURES....135
Pointers to a Struct....137
GoLang’s NESTED STRUCTURE....137
GoLang METHODS....138
Method with the Struct Type Receiver....139
Method with the Non-Struct Type Receiver....139
Methods with the Pointer Receiver....140
INTERFACES....141
How Do We Make an Interface?....142
How to Implement Interfaces....142
Why Are Go Interfaces Wonderful?....143
Redundant Functions....143
INHERITANCE....144
POLYMORPHISM USING INTERFACES....146
CHAPTER 2: Test-Driven Development....148
DEVELOPING A NEW PROJECT....149
MAKING Go FILES....149
RUN TESTS....150
A TEST THAT FAILED....150
THE FUNCTION BEING TESTED....152
WRITING A TEST-FIRST FUNCTION....152
REFACTORING Go WITH Go/ANALYSIS....154
Invoke the Refactoring Command....154
Conflicts Resolve....154
Configure the Refactoring Options....154
My Top Four Go Refactoring Approaches....155
Method of Extraction....155
Method of Move....156
Create a Parameter Object....158
Replace Symbolic Constant with Magic Number....159
REFACTORING OUR FIRST Go CODE....160
Include a Parameter Check....161
Add String Formatting....161
Take Out the Code....162
Write a Test....162
GoLang ERROR HANDLING....163
Errors in GoLang....163
Creating Our Own Mistakes....164
Errors Caused by Strings....164
Data Custom Mistakes....165
Handling Errors in Functions....166
Defer....167
Panic....169
Recover....172
Error Wrapping....173
Casting Errors....175
WRITE UNIT TESTING IN Go....176
Beginning....176
Unique to Go....177
Setup and Teardown....178
Testing within the Same Package....179
Five Basic Techniques and Strategies for Developing GoLang Unit Tests....180
CHAPTER 3: Packaging in GoLang....186
PACKAGES IN GoLang....186
Workspace....186
Main Package....187
Import Packages....188
Install Third-Party Packages....188
Init Function....189
Important Considerations....190
Giving the Packages Names....192
Code Exported....193
Essential Go Packages and Libraries....195
Awesome Go....195
GoLang-Set....195
Color....197
Now....198
Gen....199
Gorm....201
Goose....203
Glide....203
Ginkgo....204
Etcd....204
NSQ....205
Docker....207
Kubernetes....207
DEPENDENCY MANAGEMENT....207
Versioning....207
Modules....208
Installing Dependencies....209
Dependencies Must Authenticate....210
Remove Unused Dependencies....210
Installing Dependencies That Are Missing....211
Updating the Versions of Dependencies....211
Listing Dependencies....213
Module vs. Package....213
Naming....213
Fetching Packages....214
CHAPTER 4: Handling Concurrency....216
CONCURRENCY....216
Illustration of the Sleeping Barber Issue....218
Example of a Producer-Consumer Problem....221
Goroutines – CONCURRENCY IN GoLang....224
Go Concurrent Programming....224
Issues with Multithreading....224
How to Create a Goroutine....225
MULTIPLE Goroutines....225
What Is the Difference between Parallelism and Concurrency?....227
Goroutines Provide Several Advantages over Threads....227
COMMUNICATE TO SHARE MEMORY....227
HOW TO USE Goroutines TO ACCESS SHARED RESOURCES....230
Access Shared Resources Using Goroutines....230
Using Mutual Exclusion to Get Access to Shared Resources....230
Using Atomic Counters to Get Access to Shared Resources....232
Concurrency Issues....233
Concurrency Disadvantages....234
Benefits of Concurrency....234
Problems with Concurrency....234
CHAPTER 5: GoLang for JavaScript Developers....236
FLOW CONTROL....236
Loop Control Statements in Go....242
Break Statement....243
Goto Statement....243
Continue Statement....244
TYPES....245
Basic Types....245
Numbers....245
Booleans....247
Strings....248
Type Conversion....249
Type Assertion....249
Structs....251
Initializing....253
Composition vs. Inheritance....253
HOW DO YOU MAKE MODULES IN GoLang?....258
Adding Dependencies to the Go Module....260
PACKAGE MANAGEMENT USING Go MODULES....260
What Exactly Is the Go Module?....261
Starting Out....262
Module Initializing....262
Setting Up Dependencies....262
Eliminating Unnecessary Dependencies....262
Updating a Dependency Version....263
ERROR HANDLING....263
Error Type Representation....264
Various Approaches to Getting More Information from an Error....265
Don’t Ignore Errors....269
IN GoLang, THERE ARE SEVERAL TECHNIQUES TO COMPARE STRINGS....270
Making Use of Comparison Operators....270
Using the Compare() Method....272
FUNCTIONS IN Go....273
Function Declaration....273
Function Calling....274
Function Arguments....275
FUNCTION RETURNING MULTIPLE VALUES....276
Giving Names to the Return Values....277
VARIADIC FUNCTIONS....278
ANONYMOUS FUNCTION....279
GoLang main AND init FUNCTIONS....279
main() Function....280
init() Function....280
CHAPTER 6: Code Optimization....282
A PATTERN FOR OPTIMIZING Go....282
Why Is Code Optimization Necessary?....290
Code Base That Is Cleaner....291
Improved Consistency....291
Pages That Load Faster....291
Improved Code Readability....292
More Efficient Refactoring....292
Easier to Understand Debugging....292
Enhanced Workflow....292
Code Upkeep Is Now Easier....293
Improved Feature Development....293
The Ultimate Guide to GoLang Performance Optimization....293
GoLang Performance Hints That Have Been Proven and Tested....293
Optimization of Resource Efficiency....293
Optimization of Latency in GoLang....294
Efficiency of Algorithms....294
Patterns of GoLang Application Performance....294
Use Goroutines Sparingly....294
CPU Work Should Parallelize....295
Asynchronous I/O Operations....295
Keep an Eye on Our Timeouts....295
Memory Should Not Allocate in Hot Spots....295
Make Use of Lock-Free Algorithms....295
Prefer Read-Only Locks....295
Reduce or Eliminate the Use of ‘cgo’....295
Make Use of Buffered I/O....296
Use Binary Instead....296
StringBuffer and StringBuilder Are Preferable....296
Make Use of Regular Expressions....296
Slices Should Preallocate....296
Select Protocol Buffers and MessagePack as Our Options....296
For Maps, Use int Rather Than a String....296
GoLang FOR SECURE CODING....296
Stuck in a Rut with Manual Security....296
With Go, There Is a Need for Integrated, Automated Security....297
How Does Instrument-Based Security Work in the Go Pipeline?....298
Contrast Goes Above and Beyond with Modern Application Security....299
Go Security Best Practices....299
Verify the Input Entries....300
Make Use of HTML Templates....301
Protect Ourselves against SQL Injections....301
Encrypt Any Sensitive Data....302
Make HTTPS Communication Mandatory....303
Be Cautious of Mistakes and Logs....305
BEST PRACTICES FOR GoLang....306
Syntax....307
Assign Datatypes to Newly Created Variables....307
Do Not Declare a Variable or Import a Package That We Do Not Require....307
Follow Proper Naming Conventions....307
Make Use of Comments to Help in Knowledge Transfer....308
Simple, Readable, and Maintainable Code....308
Avoid Nesting by Dealing with Mistakes First....308
Avoid Utility Repeats....308
To Address Exceptional Circumstances, Use the Type Switch....308
Go Code Organization....309
For Use in CI/CD Workflows....309
Make Use of Go Modules....309
Ensure Immutability and Availability Using GOPROXY....310
CHAPTER 7: GoLang Tutorials and Projects....312
WEB APPLICATION TO GENERATE QR CODE IN GoLang....312
Install the Necessary Package....313
Development....313
Main.go Source Code....313
Generator.html Source Code....314
Implementation....314
SETTING UP THE Go HTTP CLIENT....314
HTTP Client in GoLang....315
POST and GET Requests....316
Adding Request Headers....317
Our Requests Are Being Authorized....319
WHAT IS THE GoLang REST API?....320
Using GoLang to Consume a REST API....322
Project Setup....322
Imports....322
Example of a Response....323
Struct....323
Code....323
Run the Project....324
APPRAISAL....326
CHEAT SHEET....342
BIBLIOGRAPHY....352
INDEX....356
Go, also known as GoLang, is a Google-developed open-source, compiled, and statically typed computer language. Go is a general purpose programming language with a straightforward syntax and a large standard library. The building of highly accessible and scalable web apps is one of the primary areas where GoLang is widely used. It may also be used to develop command-line programs, desktop applications, and even mobile apps.
Go was designed from the ground up for networking and infrastructure-related applications. It was developed as a replacement for popular server-side languages like Java and C++. The Go programming language aims to combine the efficiency and safety of a statically typed, compiled language with the simplicity of programming of an interpreted, dynamically typed language. It also aspires to be cutting edge, with networked and multicore computer capabilities.
GoLang is becoming one of the most popular languages, which means that learning it can open up new doors of opportunity and even help you land a job at various companies that use Go extensively.
Ease of writing concurrent programs, fast compilation, simple syntax, and static linking are some of the features that make Go an ideal candidate for developing various applications.