Mastering GoLang: A Beginner's Guide

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

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. However, 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.

Author(s): Sufyan bin Uzayr
Series: Mastering Computer Science
Publisher: CRC Press
Year: 2022

Language: English
Pages: 298
City: Boca Raton

Cover
Half Title
Series Page
Title Page
Copyright Page
Contents
Preface
About the Author
CHAPTER 1: Getting Started with Go
GO PROGRAMMING FEATURES
WHY IS GoLang BETTER THAN THE OTHER PROGRAMMING LANGUAGES?
GoLang’s Core Capability
Multithreading and Concurrency
Go Empowers Hardware from Within
The Unmatched Simplicity of Go
Inbuilt Testing and Profiling Framework
Easy Learning Curve
BEGINNING WITH Go
Text Editor
Finding a Go Compiler
INSTALL Go ON WINDOWS
How Do We Determine the Go Language Version That Is Preinstalled?
Downloading and Installing Go
WRITING THE FIRST Go PROGRAM
Single-Line Comment
Multiline Comment
Explanation of the Preceding Program
Why Is There a “Go Language”?
What Is Absent in Go That Is Present in Other Languages?
Hardware Restrictions
Benefits and Drawbacks of the Go Language
TERMINAL
The Open Terminal Tool Window
Start New Session
INSTALL Go ON MAC
Making Our First Program
Execute a Go Program
Do Programs in Go Link with the C/C++ Programming Language?
IN GoLang, HOW DO WE CREATE AN EMPTY FILE?
In GoLang, We May Check Whether a Given File Exists or Not
CREATE A DIRECTORY IN Go
Make a Single Directory
Make a Directory Hierarchy (Nested Directories)
CHAPTER 2: GoLang Tools
HOW TO READ AND WRITE PROGRAMS IN Go
IN GoLang, HOW TO RENAME AND MOVE A FILE
HOW TO READ FILES LINE BY LINE TO STRING
CHAPTER 3: Data Types
BASIC SYNTAX
Tokens
Line Separator
Comments
Identifiers
Keywords
Whitespace
DATA TYPES IN Go
Numbers
Floating Point Numbers
Complex Numbers
Booleans
Strings
CHAPTER 4: Variables and Constants
VARIABLES IN Go
Declaring a Variable
Using the var Keyword
Using the Short Variable Declaration
CONSTANTS
How Should We Declare?
Untyped and Typed Numeric Constants
Numeric Constant
String Literals
Boolean Constant
VARIABLE SCOPE IN Go
Local Variables
Global Variables
DECLARATION OF MULTIPLE VARIABLES
Shorthand Declaration
CHAPTER 5: Operators and Control Structures
OPERATORS IN Go
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
CONTROL STATEMENTS
if Statement
if…else Statement
Nested if Statement
if..else..if Ladder
Go LANGUAGE LOOPS
Go SWITCH STATEMENT
Expression Switch
Type Switch
CHAPTER 6: Arrays, Slices, and Maps
ARRAYS
Creating and Accessing an Array
Using the var Keyword
Using a Shorthand Declaration
Multidimensional Array
Important Observations about the Array
In GoLang, How Do We Copy an Array into Another Array?
In GoLang, How Can We Pass an Array to a Function?
SLICES
Slice Declaration
Slice Components
How Can We Create and Initialize a Slice?
Using the Slice Literal
Using an Array
Using an Existing Slice
Using the make() Function
How to Iterate over a Slice
Using the for loop
Using Range in the for loop
Using a Blank Identifier in a for loop
Important Points about Slice
Zero Value Slice
Modifying Slices
Slice Comparison
Multidimensional Slice
Sorting of Slice
Slice Composite Literal
In GoLang, How Do We Sort a Slice of Ints?
Ints
IntsAreSorted
In GoLang, How Can You Trim a Slice of Bytes?
How Can You Split a Slice of Bytes in GoLang?
STRINGS
String Literals
Using Double Quotes("")
Using backticks(")
Important Points about Strings
Strings Are Immutable
How to Iterate over a String
How to Access an Individual Byte of the String
How to Make a String from a Slice of Bytes
How Can We Determine the Length of a String in GoLang?
In GoLang, How Do We Trim a String?
Trim
TrimLeft
TrimRight
TrimSpace
TrimSuffix
TrimPrefix
In GoLang, How Do We Split a String?
Split
SplitAfter
SplitAfterN
In GoLang, There Are Several Ways to Compare Strings
Making Use of Comparison Operators
Using Compare() Method
MAPS
How Do We Create and Initialize Maps?
Simple
Using the make() Function
Important Considerations
How Do We Iterate over a Map?
How to Add Key-Value Pairs to the Map
How to Retrieve the Value Associated with a Key in a Map
How to Check If the Key Is Present on the Map
How to Remove a Key from a Map
Map Modification
CHAPTER 7: Functions and Recursion
Go LANGUAGE FUNCTIONS
Function Declaration
Function Calling
Function Arguments
Call by Value
Call by Reference
FUNCTION RETURNING MULTIPLE VALUES
Giving Names to the Return Values
VARIADIC FUNCTIONS
Anonymous Functions
GoLang main() and init() Functions
main() Function
init() Function
In GoLang, What Is a Blank Identifier (Underscore)?
DEFER KEYWORD
PANIC IN GoLang
Panic’s Usage
RECOVER
Quick Points
CLOSURE
RECURSION
Recursion Types
Direct Recursion
Indirect Recursion
Tail Recursion
Head Recursion
Infinite Recursion
Anonymous Function Recursion
CHAPTER 8: Pointers
GoLang POINTERS
What Is the Purpose of a Pointer?
Declaration and Initialization of Pointers
Declaring a Pointer
Pointer Initialization
Important Considerations
Dereferencing Pointer
In GoLang, How Can We Instantiate a Struct Using the New Keyword?
POINTERS TO A FUNCTION
Create a Pointer and Pass It to the Function
Passing an Address of the Variable to Function Call
POINTER TO A STRUCT
POINTER TO POINTER (DOUBLE POINTER) IN Go
How to Declare a Pointer to a Pointer
COMPARING POINTERS
CHAPTER 9: Structs and Interfaces
GoLang STRUCTURES
How Can We Get to Struct Fields?
Pointers to a Struct
GoLang’s NESTED STRUCTURE
GoLang’s ANONYMOUS STRUCTURE AND FIELD
Anonymous Structure
Anonymous Fields
GoLang METHODS
Method with the Struct Type Receiver
Method with the Non-Struct Type Receiver
Methods with the Pointer Receiver
Method Can Accept Both the Pointer and the Value
Difference between the Method and the Function
INTERFACES
How Do We Make an Interface?
How to Implement Interfaces
Why Go Interfaces Are Great
Redundant Functions
Enter Interface
EMBEDDING INTERFACES
INHERITANCE
POLYMORPHISM USING INTERFACES
CHAPTER 10: Concurrency and Goroutines
GOROUTINES – CONCURRENCY IN GoLang
Go Concurrent Programming
Issues with Multithreading
Concurrent Programming in Go
How to Handle Concurrency Issues in Go
Goroutine with WaitGroup Example
How to Create a Goroutine
Anonymous Goroutines
SELECT STATEMENT
MULTIPLE GOROUTINES
GoLang CHANNEL
Creating a Channel
Send and Receive Data from a Channel
Send Operation
Receive Operation
Channel Closing
UNIDIRECTIONAL CHANNEL
Converting a Bidirectional Channel to a Unidirectional Channel
CHAPTER 11: Packages in GoLang
PACKAGES IN GoLang
Workspace
Packages
Main Package
Importing Packages
Installing Third-Party Packages
Init Function
Important Considerations
Giving the Packages Names
Code Exported
DOCUMENTATION
CHAPTER 12: The Core Packages
STRING
INPUT/OUTPUT (I/O)
FILES AND FOLDERS
ERRORS
CONTAINERS AND SORTING
List
SORT
HASHES AND CRYPTOGRAPHY
SERVERS
HTTP
RPC
PARSING THE COMMAND LINE ARGUMENTS
SYNCHRONIZATION PRIMITIVES
Mutexes
APPRAISAL
BIBLIOGRAPHY
INDEX