Working with Concurrency in Go (Golang)

Working with Concurrency in Go (Golang)

English | MP4 | AVC 1280×720 | AAC 44KHz 2ch | 89 lectures (9h 36m) | 3.33 GB

Learn the advantages and pitfalls of concurrent programming with the Go programming language

Go, often referred to as Golang, is well-known for making it remarkably easy to work with concurrency. In order to make a particular function run concurrently, all we have to do is prepend the word “go” to the function call, and it cheerfully runs in the background, as a GoRoutine. Go’s built in scheduler takes are of making sure that a given GoRoutine runs when it should, and as efficiently as it can.

However, this does not mean that working with concurrency is simple in Go—thread safe programming takes careful planning, and most importantly it requires that developers have an absolutely solid understanding of how Go deals with concurrency.

In the standard library, Go offers us several ways of dealing with concurrently running parts of our program, right in the standard library: sync.WaitGroup, which lets us wait for tasks to finish; sync.Mutex, which allows us to lock and unlock resources, so that no two GoRoutines can access the same memory location at the same time; and finally, Channels, which allow GoRoutines to send and receive data to and from each other.

Go’s approach to concurrency is fairly straightforward, and is more or less summed up this mantra: Don’t communicate by sharing memory; instead, share memory by communicating. Channels are the means by which we usually share memory by communicating.

In this course, we’ll cover the use of WaitGroups, Mutexes, and Channels, and we’ll do so in detail. We’ll also cover some of the problems inherent in concurrency, including early program termination and race conditions. Initially, we’ll gain a good understanding of how these things work by solving some of the classic problems found in the field of computer science, including the Dining Philosophers, the Producer/Consumer problem, and the Sleeping Barber. These problems are classics for a reason: they force a developer to figure out the best approach to working with code that run concurrently, or in parallel.

Finally, we’ll finish the course out with a more “real-world” problem, where we have to register a customer for some kind of subscription service, and take care of invoicing, registration, and all the things necessary to get a new customer up and running. We’ll do so, naturally, as quickly as we can by dividing the necessary tasks up into smaller tasks, and having them run concurrently.

What you’ll learn

  • Learn about the various ways Go makes working with concurrent programing simple
  • Understand how concurrency works, and its advantages and pitfalls
  • Learn how WaitGroups, Mutexes, and channels work
  • Master concurrency by working with classic computer science problems, and by building a real-world example
Table of Contents

Introduction
1 Introduction
2 A bit about me
3 Installing Go
4 Installing Visual Studio Code
5 Installing Make
6 Asking for help
7 Mistakes. We all make them.

Goroutines, the go keyword, and WaitGroups
8 What we’ll cover in this section
9 Creating GoRoutines
10 WaitGroups to the rescue
11 Writing tests with WaitGroups
12 Challenge working with WaitGroup
13 Solution to Challenge

Race Conditions, Mutexes, and an Introduction to Channels
14 What we’ll cover in this section
15 Race Conditions an example
16 Adding sync.Mutex to our code
17 Testing for race conditions
18 A more complex example
19 Writing a test for our weekly income project
20 ProducerConsumer – Using Channels for the first time
21 Getting started with the Producer – the pizzeria function
22 Making a pizza the makePizza function
23 Finishing up the Producer code
24 Creating and running the consumer ordering a pizza
25 Finishing up our ProducerConsumer project

A Classic Problem The Dining Philosophers
26 What we’ll cover in this section
27 Getting started with the problem
28 Setting up our mutexes
29 Finishing up the code
30 Trying things out
31 Adding a delay to let a philosopher think
32 Challenge Printing out the order in which the philosophers finish eating
33 Solution to challenge
34 Writing a test for our program

Channels, and another classic The Sleeping Barber problem
35 What we’ll cover in this section
36 Introduction to channels
37 The select statement
38 Buffered Channels
39 Getting started with the Sleeping Barber project
40 Defining some variables, the barber shop, and getting started with the code
41 Adding a Barber
42 Starting the barbershop as a GoRoutine
43 Sending clients to the shop
44 Trying things out

Final Project – Building a Subscription Service
45 What we’ll cover in this section
46 Setting up a simple web application
47 Setting up our Docker development environment
48 Adding postgres
49 Setting up a Makefile
50 Adding sessions & Redis
51 Setting up the application config
52 Setting up a route & handler for the home page, and starting the web server
53 Setting up templates and building a render function
54 Adding session middleware
55 Setting up additional stub handlers and routes
56 Implementing graceful shutdown
57 Populating the database
58 Adding a data package and database models
59 Implementing the loginlogout functions

Sending Email Concurrently
60 What we’ll cover in this section
61 Getting started with the mailer code
62 Building HTML and Plain Text messages
63 Sending a message (synchronously)
64 Getting started sending a message (asynchronously)
65 Writing a helper function to send email easily
66 Sending an email on incorrect login
67 Adding cleanup tasks to the shutdown() function

Registering a User and Displaying Plans
68 What we’ll cover in this section
69 Adding mail templates and URL signer code
70 Starting on the handler to create a user
71 Activating a user
72 Giving user data to our templates
73 Displaying the Subscription Plans page
74 Adding a route and trying things out for the Plans page
75 Writing a stub handler for choosing a plan

Adding Concurrency to Choosing a Plan
76 What we’ll cover in this section
77 Getting the plan id, the plan, and the user
78 Generating an Invoice
79 Generating a manual
80 Trying things out, subscribing a user, updating the session, and redirecting

Testing
81 What we’ll cover in this section
82 Setting up our tests
83 Testing Routes
84 Testing the Renderer
85 Modifying the data package to make it testable
86 Implementing the PlanTest type
87 Getting started testing Handlers
88 Testing the Login Handler
89 Testing a handler that uses concurrency

Homepage