Cover....1
Half Title....2
Series Page....3
Title Page....4
Copyright Page....5
Contents....6
Preface....16
About the Author....18
CHAPTER 1: Getting Started with Go....20
GO PROGRAMMING FEATURES....20
WHY IS GoLang BETTER THAN THE OTHER PROGRAMMING LANGUAGES?....21
GoLang’s Core Capability....21
Multithreading and Concurrency....22
Go Empowers Hardware from Within....23
The Unmatched Simplicity of Go....23
Inbuilt Testing and Profiling Framework....23
Easy Learning Curve....24
BEGINNING WITH Go....24
Text Editor....24
Finding a Go Compiler....24
INSTALL Go ON WINDOWS....25
How Do We Determine the Go Language Version That Is Preinstalled?....25
Downloading and Installing Go....25
WRITING THE FIRST Go PROGRAM....26
Single-Line Comment....27
Multiline Comment....27
Explanation of the Preceding Program....27
Why Is There a “Go Language”?....28
What Is Absent in Go That Is Present in Other Languages?....28
Hardware Restrictions....28
Benefits and Drawbacks of the Go Language....28
TERMINAL....29
The Open Terminal Tool Window....30
Start New Session....30
INSTALL Go ON MAC....30
Making Our First Program....32
Execute a Go Program....33
Do Programs in Go Link with the C/C++ Programming Language?....33
IN GoLang, HOW DO WE CREATE AN EMPTY FILE?....34
In GoLang, We May Check Whether a Given File Exists or Not....35
CREATE A DIRECTORY IN Go....37
Make a Single Directory....37
Make a Directory Hierarchy (Nested Directories)....37
CHAPTER 2: GoLang Tools....40
HOW TO READ AND WRITE PROGRAMS IN Go....40
IN GoLang, HOW TO RENAME AND MOVE A FILE....45
HOW TO READ FILES LINE BY LINE TO STRING....46
CHAPTER 3: Data Types....48
BASIC SYNTAX....48
Tokens....48
Line Separator....49
Comments....49
Identifiers....49
Keywords....51
Whitespace....51
DATA TYPES IN Go....52
Numbers....52
Floating Point Numbers....53
Complex Numbers....53
Booleans....54
Strings....55
CHAPTER 4: Variables and Constants....56
VARIABLES IN Go....56
Declaring a Variable....57
Using the var Keyword....57
Using the Short Variable Declaration....60
CONSTANTS....64
How Should We Declare?....64
Untyped and Typed Numeric Constants....64
Numeric Constant....65
String Literals....66
Boolean Constant....67
VARIABLE SCOPE IN Go....68
Local Variables....68
Global Variables....69
DECLARATION OF MULTIPLE VARIABLES....71
Shorthand Declaration....73
CHAPTER 5: Operators and Control Structures....76
OPERATORS IN Go....76
Arithmetic Operators....77
Relational Operators....78
Logical Operators....79
Bitwise Operators....80
Assignment Operators....81
Misc Operators....83
CONTROL STATEMENTS....84
if Statement....85
if…else Statement....86
Nested if Statement....88
if..else..if Ladder....89
Go LANGUAGE LOOPS....91
Go SWITCH STATEMENT....96
Expression Switch....97
Type Switch....99
CHAPTER 6: Arrays, Slices, and Maps....102
ARRAYS....102
Creating and Accessing an Array....102
Using the var Keyword....103
Using a Shorthand Declaration....104
Multidimensional Array....105
Important Observations about the Array....106
In GoLang, How Do We Copy an Array into Another Array?....109
In GoLang, How Can We Pass an Array to a Function?....111
SLICES....112
Slice Declaration....112
Slice Components....113
How Can We Create and Initialize a Slice?....114
Using the Slice Literal....114
Using an Array....115
Using an Existing Slice....115
Using the make() Function....116
How to Iterate over a Slice....117
Using the for loop....118
Using Range in the for loop....118
Using a Blank Identifier in a for loop....119
Important Points about Slice....119
Zero Value Slice....119
Modifying Slices....119
Slice Comparison....120
Multidimensional Slice....121
Sorting of Slice....121
Slice Composite Literal....122
In GoLang, How Do We Sort a Slice of Ints?....123
Ints....123
IntsAreSorted....124
In GoLang, How Can You Trim a Slice of Bytes?....125
How Can You Split a Slice of Bytes in GoLang?....127
STRINGS....129
String Literals....130
Using Double Quotes("")....130
Using backticks(")....130
Important Points about Strings....131
Strings Are Immutable....131
How to Iterate over a String....132
How to Access an Individual Byte of the String....133
How to Make a String from a Slice of Bytes....133
How Can We Determine the Length of a String in GoLang?....134
In GoLang, How Do We Trim a String?....134
Trim....134
TrimLeft....135
TrimRight....136
TrimSpace....137
TrimSuffix....138
TrimPrefix....139
In GoLang, How Do We Split a String?....140
Split....140
SplitAfter....141
SplitAfterN....143
In GoLang, There Are Several Ways to Compare Strings....144
Making Use of Comparison Operators....144
Using Compare() Method....145
MAPS....146
How Do We Create and Initialize Maps?....147
Simple....147
Using the make() Function....148
Important Considerations....149
How Do We Iterate over a Map?....149
How to Add Key-Value Pairs to the Map....150
How to Retrieve the Value Associated with a Key in a Map....151
How to Check If the Key Is Present on the Map....151
How to Remove a Key from a Map....152
Map Modification....153
CHAPTER 7: Functions and Recursion....156
Go LANGUAGE FUNCTIONS....156
Function Declaration....156
Function Calling....157
Function Arguments....158
Call by Value....158
Call by Reference....159
FUNCTION RETURNING MULTIPLE VALUES....160
Giving Names to the Return Values....160
VARIADIC FUNCTIONS....161
Anonymous Functions....163
GoLang main() and init() Functions....166
main() Function....166
init() Function....167
In GoLang, What Is a Blank Identifier (Underscore)?....168
DEFER KEYWORD....169
PANIC IN GoLang....172
Panic’s Usage....175
RECOVER....175
Quick Points....176
CLOSURE....177
RECURSION....179
Recursion Types....180
Direct Recursion....181
Indirect Recursion....182
Tail Recursion....183
Head Recursion....184
Infinite Recursion....185
Anonymous Function Recursion....185
CHAPTER 8: Pointers....190
GoLang POINTERS....190
What Is the Purpose of a Pointer?....190
Declaration and Initialization of Pointers....192
Declaring a Pointer....192
Pointer Initialization....192
Important Considerations....193
Dereferencing Pointer....195
In GoLang, How Can We Instantiate a Struct Using the New Keyword?....196
POINTERS TO A FUNCTION....197
Create a Pointer and Pass It to the Function....197
Passing an Address of the Variable to Function Call....198
POINTER TO A STRUCT....199
POINTER TO POINTER (DOUBLE POINTER) IN Go....201
How to Declare a Pointer to a Pointer....201
COMPARING POINTERS....203
CHAPTER 9: Structs and Interfaces....206
GoLang STRUCTURES....206
How Can We Get to Struct Fields?....208
Pointers to a Struct....209
GoLang’s NESTED STRUCTURE....210
GoLang’s ANONYMOUS STRUCTURE AND FIELD....212
Anonymous Structure....212
Anonymous Fields....213
GoLang METHODS....215
Method with the Struct Type Receiver....215
Method with the Non-Struct Type Receiver....216
Methods with the Pointer Receiver....217
Method Can Accept Both the Pointer and the Value....218
Difference between the Method and the Function....219
INTERFACES....219
How Do We Make an Interface?....220
How to Implement Interfaces....220
Why Go Interfaces Are Great....225
Redundant Functions....225
Enter Interface....227
EMBEDDING INTERFACES....228
INHERITANCE....234
POLYMORPHISM USING INTERFACES....237
CHAPTER 10: Concurrency and Goroutines....240
GOROUTINES – CONCURRENCY IN GoLang....240
Go Concurrent Programming....241
Issues with Multithreading....241
Concurrent Programming in Go....242
How to Handle Concurrency Issues in Go....242
Goroutine with WaitGroup Example....242
How to Create a Goroutine....243
Anonymous Goroutines....245
SELECT STATEMENT....246
MULTIPLE GOROUTINES....250
GoLang CHANNEL....252
Creating a Channel....252
Send and Receive Data from a Channel....253
Send Operation....253
Receive Operation....253
Channel Closing....254
UNIDIRECTIONAL CHANNEL....258
Converting a Bidirectional Channel to a Unidirectional Channel....259
CHAPTER 11: Packages in GoLang....260
PACKAGES IN GoLang....260
Workspace....260
Packages....261
Main Package....261
Importing Packages....262
Installing Third-Party Packages....262
Init Function....262
Important Considerations....264
Giving the Packages Names....265
Code Exported....267
DOCUMENTATION....269
CHAPTER 12: The Core Packages....270
STRING....271
INPUT/OUTPUT (I/O)....272
FILES AND FOLDERS....272
ERRORS....275
CONTAINERS AND SORTING....275
List....275
SORT....276
HASHES AND CRYPTOGRAPHY....277
SERVERS....279
HTTP....281
RPC....282
PARSING THE COMMAND LINE ARGUMENTS....283
SYNCHRONIZATION PRIMITIVES....284
Mutexes....284
APPRAISAL....286
BIBLIOGRAPHY....290
INDEX....294
Mastering GoLang helps readers quickly understand the core concepts and then move on to practical projects using the Go programming language.
GoLang is often dubbed a game-changer in the world of programming languages. Instead of starting from scratch, Go was created using the C programming language. GoLang inherits C’s disciplined grammar but with specific tweaks and enhancements to properly manage memory. This lessens the memory leakage problems that developers tend to face with C.
Go borrows and adapts notions from various programming languages while skipping characteristics that result in complicated, insecure, and unpredictable code. Go’s concurrency features are well-suited to build the infrastructure for gigantic projects such as networking systems and distributed hardware. Go is also often employed in domains such as visuals, mobile applications, and Machine Learning.
Even though GoLang is a relatively new language, it has been adopted by several major organizations owing to its benefits, which include code clarity, custom libraries, adaptability, multithreading, and a simple build process. Because Go is gaining traction in the development community, learning GoLang can open up new avenues across various fields and career trajectories.
Since it is still a relatively newer language, quality literature pertaining to Go is often hard to find. This particular book covers all the bases that you might need, and is an ideal companion for beginner-level developers looking to master Go programming.
With Mastering GoLang, learning GoLang becomes an easy task, and learners can use their skills to create innovative projects.
The Mastering Computer Science series is edited by Sufyan bin Uzayr, a writer and educator with over a decade of experience in the computing field.