Built-in functions and keywords in Go
Discuss some very important and error-prone built-in functions and keywords in the Go language.
This article will comprehensively analyze concurrent programming in the Go language, including concurrent primitives, the use of Channels, the management of Context, the use of timers, and Go's unique GMP model.
The Go language is renowned for its powerful concurrency model, which enables developers to easily write efficient and scalable concurrent programs. In this article, we will delve into the core concepts of Go concurrency, including the use of Goroutines, the communication mechanism of Channels, the context management of Context, and Go’s unique GMP model (the scheduling model of Goroutines, threads, and processors). Through these topics, you will gain a comprehensive understanding of the design philosophy and implementation details of Go concurrency, laying a solid foundation for writing high-performance Go applications.
Goroutine is the core primitive for implementing concurrency in the Go language. It is a lightweight thread managed by the Go runtime rather than the operating system. Compared with traditional threads, the startup and context switching costs of Goroutines are extremely low, making it possible to easily create tens of thousands of Goroutines in a single application.
package main
import (
"fmt"
"time"
)
func sayHello() {
fmt.Println("Hello, Goroutine!")
}
func main() {
go sayHello() // start a new Goroutine
fmt.Println("Main function")
time.Sleep(time.Second) // make sure Goroutines have time to execute.
}
Each Goroutine has its own independent stack space. The default initial size of the stack is only 2KB. The cost of creation and destruction is very small. It is used to store local variables of function calls, return addresses, etc., and is directly managed by the runtime, allocated from the stackpool or the heap.
Stackpool is a dedicated memory pool in the runtime for managing the stack memory of Goroutines. It is completely separated from the regular heap memory allocation (mcache/mcentral/mheap) and maintains multiple stack blocks of different sizes. Each P (Processor) has a local stackpool because the stack space of each Goroutine is independent, so a lock-free design is adopted. When a Goroutine is created, it first acquires 2KB of stack memory from the local stackpool of the P. If there is none locally, it acquires a batch of stack blocks from the global stackpool to fill the local stackpool of the P. If the global stackpool also has none, it allocates new memory from the heap (mheap).
When the depth of function calls increases or there are large local variables, it may lead to insufficient stack space and cause stack expansion. By default, stack expansion still reallocates stack memory for the Goroutine from the stackpool. However, if there is no suitable free stack segment in the stackpool or a very large stack needs to be allocated, it will be directly allocated from the heap. The Go compiler performs escape analysis to try to allocate objects on the stack as much as possible and reduce heap allocation. Since stack expansion and contraction are relatively expensive operations involving memory allocation and copying, frequent stack expansion/contraction can affect performance, and deep recursion or extremely large local variables should be avoided.
When the stack space usage rate is detected to be low (such as after a large number of functions return), the Go runtime will automatically shrink the stack size and release the excess memory.
Before Go 1.4, the stack allocation algorithm used segmented stacks, which were later changed to continuous stacks. Segmented stacks were implemented by maintaining a linked list of stack segments, which was complex and could cause severe performance jitter (the Hot Split problem) when stack expansion and contraction occurred frequently in loops. To address these issues, continuous stacks were adopted. Continuous stacks are simpler to implement and, when expanding or contracting, directly allocate new continuous stack segments. This approach sacrifices the cost of instantaneous copying to achieve stable high performance.
Goroutine leakage refers to the situation where a Goroutine, after being initiated, fails to exit normally due to logical errors or design flaws, thus continuing to run and consume system resources. This issue is particularly common in high-concurrency programs and may lead to memory leaks, performance degradation, and even program crashes.
// Common scenarios of Goroutine leaks
// Blocked Channel operations
func main() {
ch := make(chan int)
go func() {
<-ch // The blockage here causes leakage.
}()
// The main Goroutine did not close ch, so the child Goroutine can never exit.
}
// Infinite loop
func main() {
go func() {
for {
// Infinite loop
}
}()
}
// Unprocessed timeout
func main() {
ch := make(chan int)
go func() {
select {
case <-ch:
// Handle normally
}
// Without timeout handling, it may lead to leakage.
}()
}
To avoid Goroutine leaks, we can follow some best practices for using Goroutines.
// Using context to control the lifecycle of Goroutines
func main() {
ctx, cancel := context.WithCancel(context.Background())
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("Goroutine exiting")
return
default:
if err := ctx.Err(); err != nil {
return // Early exit
}
// Carry out the mission
}
}
}(ctx)
time.Sleep(time.Second)
cancel() // Notify the Goroutine to exit.
}
// Make sure the Channel is closed properly.
func main() {
ch := make(chan int)
go func() {
for val := range ch {
fmt.Println(val)
}
}()
ch <- 1
close(ch) // Make sure to close the Channel.
}
// Set a timeout mechanism
func main() {
ch := make(chan int)
go func() {
select {
case <-ch:
fmt.Println("Received data")
case <-time.After(time.Second): // Timeout handling
fmt.Println("Timeout")
}
}()
}
Go provides some powerful concurrency primitives for communication and synchronization among Goroutines. These primitives are at the core of Go’s concurrency model and help developers write efficient and safe concurrent programs.
sync.Mutex and sync.RWMutex are two lock implementations provided by the Go standard library, used to control concurrent access to shared resources.
sync.Mutex is a mutual exclusion lock that only allows one Goroutine to hold the lock at a time. Other Goroutines attempting to acquire the lock will be blocked.
package main
import (
"fmt"
"sync"
)
func main() {
var mu sync.Mutex
count := 0
for i := 0; i < 5; i++ {
go func() {
mu.Lock()
count++
mu.Unlock()
}()
}
}
Before Go 1.9, the implementation of sync.Mutex was a non-fair lock, meaning that the order in which Goroutines acquired the lock was not consistent with the order in which they arrived. Some Goroutines might be unable to obtain the lock for a long time, leading to starvation issues. After Go 1.9, sync.Mutex was improved to be switchable between fair and non-fair locks. When the waiting time of Goroutines in the queue exceeds 1 ms, the lock enters starvation mode. New arriving Goroutines will not attempt to spin to acquire the lock but directly join the waiting queue. When the lock is released, it will be directly handed over to the Goroutine at the head of the waiting queue. After the last waiting Goroutine acquires the lock, the lock exits starvation mode. Non-fair locks perform better in low contention scenarios, but in high contention scenarios, they may cause starvation issues and have unpredictable response times. Fair locks, although generally having average performance in low contention scenarios, avoid starvation issues in high contention scenarios and have more balanced response times.
The underlying implementation of sync.Mutex relies on the atomic operations and thread scheduling mechanisms of the operating system. Its core structure is as follows:
type Mutex struct {
state int32
sema uint32
}
The state field represents the lock’s status, with the lowest three bits respectively indicating whether it is held (mutexLocked), whether it has been awakened from the normal mode (mutexWoken), and whether it is in a starved state (mutexStarving). The remaining bits are used to represent the number of Goroutines waiting for the lock to be released; sema represents the semaphore, which is used for blocking and waking up Goroutines.
func (m *Mutex) Lock() {
if atomic.CompareAndSwapInt32(&m.state, 0, 1) {
// The lock was successfully acquired.
return
}
// Failed to acquire the lock, blocking the current Goroutine.
runtime_Semacquire(&m.sema)
}
func (m *Mutex) Unlock() {
if atomic.AddInt32(&m.state, -1) == 0 {
// Return directly without waiting for the Goroutine.
return
}
// Wake up one Goroutine in the waiting queue.
runtime_Semrelease(&m.sema)
}
sync.RWMutex is a read-write lock that allows multiple read operations or a single write operation to occur simultaneously at the same time.
var rwmu sync.RWMutex
var data map[string]string
func read(key string) string {
rwmu.RLock()
defer rwmu.RUnlock()
return data[key]
}
func write(key, value string) {
rwmu.Lock()
defer rwmu.Unlock()
data[key] = value
}
Its implementation is based on sync.Mutex and coordinates read and write operations through internal state fields and semaphores.
type RWMutex struct {
w sync.Mutex // Writer mutex
writerSem uint32 // Semaphore for write operation
readerSem uint32 // Semaphore for read operation
readerCount int32 // The number of Goroutines currently performing read operations
readerWait int32 // The number of pending read operations
}
Read lock implementation
func (rw *RWMutex) RLock() {
if atomic.AddInt32(&rw.readerCount, 1) < 0 {
// If readerCount is less than 0, it indicates that a write lock exists and the current Goroutine will be blocked.
runtime_Semacquire(&rw.readerSem)
}
}
func (rw *RWMutex) RUnlock() {
if r := atomic.AddInt32(&rw.readerCount, -1); r < 0 {
// If readerCount < 0, it indicates that there is a write lock waiting; wake up the write lock.
if atomic.AddInt32(&rw.readerWait, -1) == 0 {
runtime_Semrelease(&rw.writerSem)
}
}
}
Write lock implementation
func (rw *RWMutex) Lock() {
rw.w.Lock() // Obtain a write lock
r := atomic.AddInt32(&rw.readerCount, -rwmutexMaxReaders) // Prevent new read operations
if r != 0 {
// If there is a current read operation, wait for all read operations to complete.
atomic.AddInt32(&rw.readerWait, r)
runtime_Semacquire(&rw.writerSem)
}
}
func (rw *RWMutex) Unlock() {
r := atomic.AddInt32(&rw.readerCount, rwmutexMaxReaders) // Resume morning exercises.
for i := 0; i < int(r); i++ {
runtime_Semrelease(&rw.readerSem) // Wake up all waiting read operations.
}
rw.w.Unlock() // Release the write lock
}
The read-write lock in Go is a fair read-write lock. When a Goroutine attempts to acquire a write lock, it blocks subsequent write lock acquisitions and then subsequent read lock acquisitions. If there are still Goroutines holding read locks at this time, the Goroutine will go to sleep and wait for the read locks to be released. When the Goroutine releases the write lock, it first releases the read locks and wakes up all Goroutines waiting for read locks, and then releases the write lock. This ensures that read operations will not be starved by consecutive write operations. Therefore, sync.RWMutex performs better in scenarios with more reads and fewer writes, while in scenarios with balanced reads and writes, using sync.Mutex is simpler and more efficient.
The TryLock method was newly added in Go 1.18.
var mu sync.Mutex
if mu.TryLock() {
defer mu.Unlock()
// The lock was acquired successfully.
} else {
// Failed to acquire the lock.
}
When using locks, it is important to avoid nested locks to prevent deadlocks. At the same time, control the granularity of the locks and only lock shared data to reduce lock contention and improve concurrent performance.
The sync.WaitGroup is used to wait for a group of Goroutines to complete.
package main
import (
"fmt"
"sync"
)
func main() {
var wg sync.WaitGroup
for i := 0; i < 3; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
fmt.Println("Goroutine", i)
}(i)
}
wg.Wait()
fmt.Println("All Goroutines finished")
}
Its implementation principle is based on the counter and semaphore mechanism. The core idea is to record the number of unfinished Goroutines through a counter and wake up the waiting Goroutines when the counter reaches zero.
type WaitGroup struct {
state1 [3]uint32 // Including counters and semaphores
sema uint32 // Semaphores are used to block and wake up Goroutines.
}
The Add method is used to increase or decrease the value of the counter. When the counter reaches zero, it wakes up the blocked Goroutine through a semaphore. The Done method is a simplified call to Add(-1) and is used to decrease the counter. The Wait method blocks the calling Goroutine until the counter reaches zero.
func (wg *WaitGroup) Add(delta int) {
state := atomic.AddUint32(&wg.state1[0], uint32(delta))
if int32(state) < 0 {
panic("sync: negative WaitGroup counter")
}
if delta < 0 && state == 0 {
runtime_Semrelease(&wg.sema) // Wake up the waiting Goroutine
}
}
func (wg *WaitGroup) Done() {
wg.Add(-1)
}
func (wg *WaitGroup) Wait() {
if atomic.LoadUint32(&wg.state1[0]) == 0 {
return // If the counter is 0, return directly.
}
runtime_Semacquire(&wg.sema) // Block the current Goroutine and wait for the semaphore to be released.
}
sync.Cond provides a mechanism for Goroutines to wait and notify, which is suitable for complex synchronization scenarios.
package main
import (
"fmt"
"sync"
)
func main() {
cond := sync.NewCond(&sync.Mutex{})
ready := false
go func() {
cond.L.Lock()
for !ready {
cond.Wait()
}
fmt.Println("Condition met")
cond.L.Unlock()
}()
cond.L.Lock()
ready = true
cond.Signal()
cond.L.Unlock()
}
The core structure of sync.Cond is as follows:
type Cond struct {
L Locker // The associated lock, typically a sync.Mutex or sync.RWMutex.
notify notifyList // An internal notification list for managing waiting Goroutines.
checker copyChecker // Prevent Cond from being wrongly copied.
}
The Wait method will put the current Goroutine into a waiting state until it is awakened by another Goroutine.
func (c *Cond) Wait() {
t := runtime_notifyListAdd(&c.notify) // Add the current Goroutine to the notification list.
c.L.Unlock() // Release the associated lock.
runtime_notifyListWait(&c.notify, t) // Block the current Goroutine and wait to be awakened.
c.L.Lock() // Reacquire the lock after being awakened.
}
The Signal method will wake up a waiting Goroutine.
func (c *Cond) Signal() {
runtime_notifyListNotifyOne(&c.notify) // Wake up a Goroutine in the notification list.
}
The Broadcast method will wake up all waiting Goroutines.
func (c *Cond) Broadcast() {
runtime_notifyListNotifyAll(&c.notify) // Wake up all the Goroutines in the notification list.
}
It should be noted that when invoking Wait, Signal or Broadcast, the associated lock L must be acquired first. Additionally, to prevent issues caused by spurious wakeups, where a thread is awakened from the waiting state without a clear notification or signal, it is usually necessary to use a for loop to repeatedly check the condition while waiting, rather than simply relying on a single wait.
sync.Once ensures that a certain piece of code is executed only once, and is typically used in singleton patterns or initialization operations.
package main
import (
"fmt"
"sync"
)
func main() {
var once sync.Once
for i := 0; i < 3; i++ {
go func() {
once.Do(func() {
fmt.Println("This will only run once")
})
}()
}
}
The implementation of sync.Once relies on atomic operations and memory barriers, ensuring thread safety in a multi-Goroutine environment.
type Once struct {
done uint32 // Flag bit, indicating whether it has been executed or not.
m sync.Mutex // Mutual exclusion lock, used to protect the execution of functions.
}
The Do method is used to execute the specified function f and ensures that the function is executed only once.
func (o *Once) Do(f func()) {
if atomic.LoadUint32(&o.done) == 1 { // Quick path: Check if it has been executed.
return
}
o.m.Lock() // Lock to ensure that only one Goroutine is executed.
defer o.m.Unlock()
if o.done == 0 { // Check again to prevent other Goroutines from having already executed.
defer atomic.StoreUint32(&o.done, 1) // Mark as executed.
f() // Execute the objective function
}
}
Channel is the core of Go’s concurrent model and is used to pass data between Goroutines. It provides a type-safe and thread-safe communication mechanism, enabling Goroutines to collaborate through message passing rather than shared memory.
We can declare a Channel without initializing it. In this case, the Channel will be nil. Performing send or receive operations on a nil Channel will cause a permanent block, and closing a nil Channel will trigger a panic. In Channel operations, apart from closing a nil Channel which will trigger a panic, closing or sending to a closed Channel will also trigger a panic. Additionally, closing a Channel that is only for receiving will trigger a panic.
Channels can be created using the make function, and it is possible to specify whether they are buffered or not. Unbuffered Channels will block send and receive operations until the other end is ready. For buffered Channels, if the buffer is full, the send operation will be blocked until a receive operation occurs; if the buffer is empty, the receive operation will be blocked until a send operation occurs. A closed Channel cannot perform send operations, but it can still execute receive operations until all data in the buffer is retrieved. After the buffer is empty, receive operations will only receive the zero value of the Channel’s element type.
The implementation of Channel is based on the scheduler of the Go runtime, integrating queues, locks, and the blocking and waking mechanisms of Goroutines. The core structure is defined as follows:
type hchan struct {
qcount uint // The current number of elements in the queue
dataqsiz uint // The capacity of the queue (buffer size)
buf unsafe.Pointer // The pointer of the circular buffer
elemsize uint16 // The size of each element
closed uint32 // Has the channel been closed
sendx uint // The send index of the circular buffer
recvx uint // The receiving index of the circular buffer
recvq waitq // The queue of Goroutines waiting to be received
sendq waitq // The queue of Goroutines waiting to be sent
lock mutex // Mutual exclusion lock, protecting concurrent access to the Channel
}
Application scenarios
Producer-Consumer Pattern: Use Channel to implement communication between producers and consumers.
package main
import "fmt"
func producer(ch chan int) {
for i := 0; i < 5; i++ {
ch <- i
}
close(ch)
}
func consumer(ch chan int) {
for val := range ch {
fmt.Println("Consumed:", val)
}
}
func main() {
ch := make(chan int)
go producer(ch)
consumer(ch)
}
Work pool pattern: Use multiple Goroutines to handle tasks and distribute tasks through Channels.
package main
import (
"fmt"
"sync"
)
func worker(id int, ch <-chan int, wg *sync.WaitGroup) {
defer wg.Done()
for task := range ch {
fmt.Printf("Worker %d processing task %d\n", id, task)
}
}
func main() {
tasks := make(chan int, 10)
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
wg.Add(1)
go worker(i, tasks, &wg)
}
for i := 1; i <= 10; i++ {
tasks <- i
}
close(tasks)
wg.Wait()
}
Timeout control: Implement timeout control using time.After and Channel.
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
go func() {
time.Sleep(2 * time.Second)
ch <- 42
}()
select {
case val := <-ch:
fmt.Println("Received:", val)
case <-time.After(1 * time.Second):
fmt.Println("Timeout")
}
}
Limit concurrency: Use a buffered channel as a semaphore.
var sem = make(chan int, 10)
func process(r *Request) {
sem <- 1
processRequest(r)
<-sem
}
Pipeline: Build a data processing pipeline.
func gen(nums ...int) <-chan int {
out := make(chan int)
go func() {
for _, n := range nums {
out <- n
}
close(out)
}()
return out
}
func sq(in <-chan int) <-chan int {
out := make(chan int)
go func() {
for n := range in {
out <- n * n
}
close(out)
}()
return out
}
nums := gen(2, 3, 4)
squares := sq(nums)
The Context interface was introduced in the Go 1.7 standard library and is a core tool in Go for controlling the lifecycle of Goroutines. It is particularly suitable for scenarios where cancellation signals need to be passed between multiple Goroutines, timeouts need to be controlled, or data needs to be shared.
This interface defines four methods that need to be implemented:
The functions context.Background, context.TODO, context.WithCancel, context.WithDeadline, context.WithTimeout, and context.WithValue provided in the context package return private struct types that implement this interface.
Both context.Background and context.TODO create an emptyCtx, which is an uncancelable context with no deadline. However, they have different meanings. context.Background is the default value for contexts, and all other contexts should be derived from it. On the other hand, context.TODO should only be used when it is not clear which context to use.
type emptyCtx int
func (e emptyCtx) Deadline() (time.Time, bool) { return time.Time{}, false }
func (e emptyCtx) Done() <-chan struct{} { return nil }
func (e emptyCtx) Err() error { return nil }
func (e emptyCtx) Value(key interface{}) interface{} { return nil }
The context.WithCancel function creates a cancelCtx, which includes a done channel for transmitting cancellation signals.
type cancelCtx struct {
Context
done chan struct{} // The Channel used for cancellation notifications
mu sync.Mutex // Mutual exclusion lock for protecting operations
err error // The reason for the cancellation
}
Both context.WithTimeout and context.WithDeadline create a timerCtx, which is implemented based on cancelCtx. It includes a deadline time and a timer that automatically cancels the context when the timeout occurs.
type timerCtx struct {
cancelCtx
deadline time.Time
timer *time.Timer
}
The context.WithValue method creates a valueCtx that contains a set of key-value pairs.
type valueCtx struct {
Context
key, val interface{}
}
Application scenarios
Control the lifecycle of Goroutines. By using the cancellation signal of Context, Goroutines can be gracefully terminated to avoid resource leaks.
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("Goroutine exiting")
return
default:
fmt.Println("Working...")
time.Sleep(500 * time.Millisecond)
}
}
}(ctx)
time.Sleep(2 * time.Second)
cancel()
time.Sleep(1 * time.Second)
}
Timeout control. In scenarios such as network requests or database queries, use WithTimeout or WithDeadline to set the timeout period.
package main
import (
"context"
"fmt"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
select {
case <-time.After(3 * time.Second):
fmt.Println("Operation completed")
case <-ctx.Done():
fmt.Println("Timeout:", ctx.Err())
}
}
Pass the data of the request scope. In HTTP request processing, WithValue can be used to pass data between different middleware or processors.
package main
import (
"context"
"fmt"
)
func main() {
ctx := context.WithValue(context.Background(), "userID", 12345)
processRequest(ctx)
}
func processRequest(ctx context.Context) {
userID := ctx.Value("userID")
fmt.Println("Processing request for user:", userID)
}
The scheduler of Go is one of the core components of the Go runtime, responsible for managing the execution of Goroutines. Go employs a scheduling mechanism known as the GMP model, which can efficiently utilize multi-core CPUs while maintaining the lightweight nature of Goroutines.
The GMP model consists of the following three core components:
Each P maintains a local Goroutine queue. M needs to be bound to a P to execute Goroutines. G is allocated by P for M to execute.
Creating Goroutines:
Scheduling Goroutines:
Blocking Goroutines:
Preemptive Scheduling:
In the previous text, the stack space management of Goroutines has been discussed. Here, we mainly focus on the task status. In Go’s GMP scheduling model, the status of Goroutines is the core part managed by the scheduler. Each Goroutine has a status that describes its current execution situation. The following are the 9 main states of Goroutines:
_Gidle (Idle State)
_Grunnable (Runnable State)
_Grunning (Running State)
_Gsyscall (System Call State)
_Gwaiting (Waiting State)
_Gdead (Dead State)
_Gcopystack (Stack Copying State)
_Gpreempted (Preempted State)
_Gscan (Scanning State)
The following is the typical transition process of Goroutine states:
Creating a Goroutine:
_Gidle.go keyword is called, its state changes to _Grunnable and it is added to the scheduling queue.Scheduling and Execution:
_Grunnable Goroutine to a thread (M), and its state changes to _Grunning.Blocking or Waiting:
_Gwaiting._Gsyscall.Preemption or Suspension:
_Gpreempted.Completion and Execution:
_Gdead and it awaits garbage collection.The runtime tools of Go (such as pprof or trace) can be used to view the status of Goroutines.
Trace tracks the execution process of a program, analyzing Goroutine scheduling, system calls, garbage collection, etc., and is suitable for debugging concurrent issues and analyzing scheduling behavior. Pprof analyzes the performance bottlenecks of a program, such as CPU usage and memory allocation, and is suitable for optimizing performance and locating hotspots of resource consumption.
package main
import (
"fmt"
"os"
"runtime/trace"
)
func main() {
f, err := os.Create("trace.out")
if err != nil {
fmt.Println("Failed to create trace file:", err)
return
}
defer f.Close()
if err := trace.Start(f); err != nil {
fmt.Println("Failed to start trace:", err)
return
}
defer trace.Stop()
// Your program logic here
fmt.Println("Hello, trace!")
}
go tool trace trace.out
package main
import (
_ "net/http/pprof"
"net/http"
)
func main() {
go func() {
http.ListenAndServe("localhost:6060", nil)
}()
// Your program logic here
select {}
}
// Obtain 30 seconds of CPU performance analysis data.
curl -o cpu.prof http://localhost:6060/debug/pprof/profile
go tool pprof cpu.prof
top: Show the most time-consuming functions.
list: View detailed information about a specific function.
web: Generate graphical performance analysis reports (Graphviz installation required).
