Tucker's Golang programming Ch23 ~ Ch26
This post is a translation of the original Korean post.
This text is a summary of chapters 23 to 26 of the book “Golden Rabbit: [ Tucker’s Golang programming ]”.
Ch23 ~ Ch26
- Error Handling
- GoRoutine and Concurrent Programming
- Channels and Contexts
- Creating a Word Search Program
# Error Handling
# Error type
|
|
Can be used as an error if you implement an Error()
method that returns a string
# Generate an error
Create a user error
|
|
Wrapping an error
- Wrap an error to create a new error
|
|
# panic
Ability to stop program flow (crash)
- Terminate the program immediately to quickly see when something goes wrong
- Can check the call stack (order of function calls)
|
|
recover()
Recovers a panic from terminating the program.
- Returns the panic object that occurred
|
|
# Goroutine
Lightweight threads managed by the Go language
- Used to execute functions or commands simultaneously
|
|
Main Routine
Starts and ends with the main() function (also exits the program)
- Subroutines created from the main() function also exit immediately when the main() function exits.
# WaitGroup
Used to wait for a group to exit from a goroutine
|
|
# How it works
Lightweight threads using OS threads
- Allocate only one OS thread per CPU core and use it.
- When there are no cores available, the goroutines wait until there are.
- When a threaded goroutine enters a wait state (e.g. listening to the network), it swaps with the waiting goroutines.
Advantages.
No context switching costs (the CPU core doesn’t change threads, it just moves the goroutines)
# Concurrency programming
# Mutexes
Mutual exclusion
- Controlling access to resources
|
|
Disadvantages
- No performance gains from concurrent programming > - Only one goroutine can access the shared resource
- Potential for deadlocks > - Make sure it doesn’t deadlock and use it in a narrow scope.
# Resource management techniques
Manage resources so that goroutines do not access the same resource
- How to divide the area
- Allocate goroutines by e.g. file
- How to divide roles
- Using Channels.
# Channel and context
# Channels
Message queue for passing messages between goroutines
Create an instance
|
|
Using
|
|
# range statement
Get all the values of the channel and escape if it’s closed
|
|
# select statement
|
|
- Waiting on multiple channels simultaneously
- Use with a for statement to continue processing data without exiting
- Use inside a goroutine to wait on a channel while doing something else
time channel
- time.Tick(): Creates a channel that sends a signal at regular time intervals cycle.
- time.After(): Create a channel that sends a signal after a certain amount of time after.
# Producer Consumer Pattern
How to divide roles (conveyor belt system)
Put channels between goroutines(tasks) and pass the results of the tasks to the channels.
advantages
Instead of waiting for all tasks to finish before starting the next one. you can start your own task
# Context
Function provided by the context package to act as a task statement.
Context for canceling tasks
|
|
Context in which the task time was set
|
|
Context with a specific value set
|
|
context wrapping
When creating a context, pass an already created context object as an argument.
context wrapping is possible
# Create a word search program
Structure
Code
|
|