首页 > 后端开发 > 戈兰 > 正文

过于简化的 Golang 通道!

WBOY
发布: 2024-07-28 14:07:13
原创
938 人浏览过

Oversimplified Golang Channel!

长话短说

文章解释了 Go 通道,它可以实现 goroutine 之间的安全通信。它涵盖了如何通过通道创建、发送和接收数据,区分无缓冲和缓冲类型。它强调了关闭渠道以防止僵局和改善资源管理的重要性。最后,介绍了用于高效管理多个通道操作的 select 语句。


目录

  1. Go Channel 简介
  2. 创建频道
  3. 发送数据
  4. 接收数据
  5. Go 中的频道类型
    • 无缓冲通道
    • 缓冲通道
  6. 关闭频道
    • 为什么要关闭频道?
  7. 不关闭通道的代码片段
    • 预期输出和错误
  8. 关闭通道的代码片段
    • 预期输出
  9. 使用 select 语句
    • 使用通道进行选择的示例
  10. 选择常见问题解答
  11. 使用 WaitGroup 确保来自两个通道的消息
    • 使用 WaitGroup 的示例
  12. 结论

Go 通道简介

Go(或 Golang)是一种功能强大的编程语言,旨在简单和高效。它的突出特点之一是通道的概念,它促进了 goroutine 之间的通信。通道允许安全的数据交换和同步,使并发编程更容易、更易于管理。

在本文中,我们将探索 Go 中的通道,分解它们的创建、数据传输和接收。这将帮助您了解如何在应用程序中有效地利用渠道。

创建频道

要在 Go 中创建通道,请使用 make 函数。这是一个简单的代码片段,演示了如何创建频道:

雷雷

在这个例子中,我们创建了一个可以发送和接收整数的通道 ch。默认情况下,通道是无缓冲的,这意味着它将阻塞,直到发送者和接收者都准备好为止。

当您运行提供的 Go 代码时,输​​出将如下所示:

雷雷

解释

  1. 频道创建

    • 行 ch := make(chan int) 创建一个 int 类型的新通道。该通道可用于发送和接收整数值。
  2. 频道地址:

    • 输出0xc000102060是通道的内存地址。在 Go 中,当您打印通道时,它会显示其内部表示,其中包括其在内存中的地址。
    • 此地址指示通道在内存中存储的位置,但它不提供有关通道状态或内容的任何信息。

发送数据

创建通道后,您可以使用

雷雷

在此代码片段中,我们启动一个新的 goroutine,将整数值 42 发送到通道 ch 中。这种异步操作允许主程序在发送值的同时继续执行。

接收数据

要从通道接收数据,您还可以使用

雷雷

在此示例中,我们从通道 ch 读取并将接收到的值存储在变量 value 中。程序将阻塞在这一行,直到有值可供读取。

Go 中的通道类型

在Go中,通道主要分为两种类型:无缓冲通道和缓冲通道。了解这些类型对于有效的并发编程至关重要。

1. 无缓冲通道

无缓冲通道是最简单的类型。它没有任何保存数据的能力;它要求发送者和接收者同时准备好。

特征:

  • 阻塞行为:发送和接收操作会阻塞,直到双方都准备好为止。这确保了 goroutine 之间的同步。
  • 使用案例:最适合需要严格同步或通信不频繁的场景。

例子:

雷雷

2. 缓冲通道

缓冲通道允许您指定容量,这意味着它们在阻止发送之前可以保存有限数量的值。

Characteristics:

  • Non-blocking Sends: A send operation only blocks when the buffer is full. This allows for greater flexibility and can improve performance in certain scenarios.
  • Use Case: Useful when you want to decouple the sender and receiver, allowing the sender to continue executing until the buffer is filled.

Example:

ch := make(chan int, 2) // Buffered channel with capacity of 2
ch <- 1 // Does not block
ch <- 2 // Does not block
// ch <- 3 // Would block since the buffer is full
fmt.Println("Values sent to buffered channel.")
登录后复制

What is Closing a Channel?

In Go, closing a channel is an operation that signals that no more values will be sent on that channel. This is done using the close(channel) function. Once a channel is closed, it cannot be reopened or sent to again.

Why Do We Need to Close Channels?

  1. Signal Completion: Closing a channel indicates to the receiving goroutine that no more values will be sent. This allows the receiver to know when to stop waiting for new messages.

  2. Preventing Deadlocks: If a goroutine is reading from a channel that is never closed, it can lead to deadlocks where the program hangs indefinitely, waiting for more data that will never arrive.

  3. Resource Management: Closing channels helps in managing resources effectively, as it allows the garbage collector to reclaim memory associated with the channel once it is no longer in use.

  4. Iteration Control: When using a for range loop to read from a channel, closing the channel provides a clean way to exit the loop once all messages have been processed.

In this section, we will explore a Go code snippet that demonstrates the use of unbuffered channels. We will analyze the behavior of the code with and without closing the channel, as well as the implications of each approach.

Code Snippet Without Closing the Channel

Here’s the original code snippet without the close statement:

package main

import (
    "fmt"
)

func main() {
    messages := make(chan string)

    go func() {
        messages <- "Message 1"
        messages <- "Message 2"
        messages <- "Message 3"
        // close(messages) // This line is removed
    }()

    for msg := range messages {
        fmt.Println(msg)
    }
}
登录后复制

Expected Output and Error

fatal error: all goroutines are asleep - deadlock!
登录后复制

When you run this code, it will compile and execute, but it will hang indefinitely without producing the expected output. The reason is that the for msg := range messages loop continues to wait for more messages, and since the channel is never closed, the loop has no way of knowing when to terminate. This results in a deadlock situation, causing the program to hang.

Code Snippet With Closing the Channel

Now, let’s add the close statement back into the code:

package main

import (
    "fmt"
)

func main() {
    messages := make(chan string)

    go func() {
        messages <- "Message 1"
        messages <- "Message 2"
        messages <- "Message 3"
        close(messages) // Close the channel when done
    }()

    for msg := range messages {
        fmt.Println(msg)
    }
}
登录后复制

Expected Output

With the close statement included, the output of this code will be:

Message 1
Message 2
Message 3
登录后复制

Explanation of Closure Behavior

In this version of the code:

  • The close(messages) statement signals that no more messages will be sent on the messages channel.
  • The for msg := range messages loop can now terminate gracefully once all messages have been received.
  • Closing the channel allows the range loop to exit after processing all messages, preventing any deadlock situation.

Again, what if you don't close the channel?

Let's imagine a scenario where channels in Go are like people in a conversation.


Scene: A Coffee Shop

Characters:

  • Alice: Always eager to share ideas.
  • Bob: Takes a long time to respond.

Conversation:

Alice: "Hey Bob, did you hear about the new project? We need to brainstorm!"

Bob sips his coffee, staring blankly. The conversation is paused.

Alice: "Hello? Are you there?"

Bob looks up, still processing.

Bob: "Oh, sorry! I was... uh... thinking."

Minutes pass. Alice starts to wonder if Bob is even still in the chat.

Alice: "Should I keep talking or just wait for a signal?"

Bob finally responds, but it’s completely off-topic.

Bob: "Did you know that sloths can hold their breath longer than dolphins?"

Alice facepalms.

Alice: "Great, but what about the project?"

Bob shrugs, lost in thought again. The coffee shop becomes awkwardly silent.

Alice: "Is this conversation ever going to close, or will I just be here forever?"

Bob, now fascinated by the barista, mutters something about coffee beans.

Alice: "This is like a Go channel that never gets closed! I feel like I’m stuck in an infinite loop!"

Bob finally looks back, grinning.

Bob: "So... about those sloths?"


Moral of the Story: Sometimes, when channels (or conversations) don’t close, you end up with endless topics and no resolution—just like a chat that drags on forever without a conclusion!

Go Channels and the select Statement

Go's concurrency model is built around goroutines and channels, which facilitate communication between concurrent processes. The select statement is vital for managing multiple channel operations effectively.

Using select with Channels

Here's an example of using select with channels:

package main

import (
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan string)
    ch2 := make(chan string)

    go func() {
        time.Sleep(1 * time.Second)
        ch1 <- "Result from channel 1"
    }()

    go func() {
        time.Sleep(2 * time.Second)
        ch2 <- "Result from channel 2"
    }()

    select {
    case msg1 := <-ch1:
        fmt.Println(msg1)
    case msg2 := <-ch2:
        fmt.Println(msg2)
    }
}
登录后复制

Output with select:

Result from channel 1
登录后复制

Why Does It Print Only One Output?

In Go, the select statement is a powerful construct used for handling multiple channel operations. When working with channels, you might wonder why a program prints only one output when multiple channels are involved. Let’s explore this concept through a simple example.

Scenario Overview

Consider the program that involves two channels: ch1 and ch2. Each channel receives a message after a delay, but only one message is printed at the end. You might ask, "Why does it only print one output?"

Timing and Concurrency

  1. Channel Initialization: Both ch1 and ch2 are created to handle string messages.

  2. Goroutines:

    • A goroutine sends a message to ch1 after a 1-second delay.
    • Another goroutine sends a message to ch2 after a 2-second delay.
  3. Select Statement: The select statement listens for messages from both channels. It blocks until one of the channels is ready to send a message.

Execution Flow

  • When the program runs, it waits for either ch1 or ch2 to send a message.
  • After 1 second, ch1 is ready, allowing the select statement to execute the case for ch1.
  • Importantly, select can only execute one case at a time. Once a case is selected, it exits the select block.

FAQ on select

Q: Is it possible to wait for all channels in select to print all outputs?

A: No, the select statement is designed to handle one case at a time. To wait for multiple channels and print all outputs, you would need to use a loop or wait group.

Q: What happens if both channels are ready at the same time?

A: If both channels are ready simultaneously, Go will choose one at random to process, so the output may vary between executions.

Q: Can I handle timeouts with select?

A: Yes, you can include a timeout case in the select statement, allowing you to specify a duration to wait for a message.

Q: How can I ensure I receive messages from both channels?

A: To receive messages from both channels, consider using a loop with a select statement inside it, or use a sync.WaitGroup to wait for multiple goroutines to complete their tasks.

Ensuring Messages from Both Channels Using WaitGroup in Go

To ensure you receive messages from both channels in Go, you can use a sync.WaitGroup. This allows you to wait for multiple goroutines to complete before proceeding.

Here’s an example:

package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {
    ch1 := make(chan string)
    ch2 := make(chan string)
    var wg sync.WaitGroup

    // Start goroutine for channel 1
    wg.Add(1)
    go func() {
        defer wg.Done()
        time.Sleep(1 * time.Second)
        ch1 <- "Result from channel 1"
    }()

    // Start goroutine for channel 2
    wg.Add(1)
    go func() {
        defer wg.Done()
        time.Sleep(2 * time.Second)
        ch2 <- "Result from channel 2"
    }()

    // Wait for both goroutines to finish
    go func() {
        wg.Wait()
        close(ch1)
        close(ch2)
    }()

    // Collect results from both channels
    results := []string{}
    for i := 0; i < 2; i++ {
        select {
        case msg1 := <-ch1:
            results = append(results, msg1)
        case msg2 := <-ch2:
            results = append(results, msg2)
        }
    }

    // Print all results
    for _, result := range results {
        fmt.Println(result)
    }
}
登录后复制

Output

Result from channel 1
Result from channel 2
登录后复制

Explanation

  1. Channels and WaitGroup: Two channels, ch1 and ch2, are created. A sync.WaitGroup is used to wait for both goroutines to finish.

  2. Goroutines: Each goroutine sends a message to its channel after a delay. The wg.Done() is called to signal completion.

  3. Closing Channels: After all goroutines are done, the channels are closed to prevent any further sends.

  4. Collecting Results: A loop with a select statement is used to receive messages from both channels until both messages are collected.

  5. Final Output: The collected messages are printed.

This method ensures that you wait for both channels to send their messages before proceeding.

If you're interested in learning more about using sync.WaitGroup in Go, check out this article on concurrency: Golang Concurrency: A Fun and Fast Ride.

Real world example

Let's compare the two versions of a program in terms of their structure, execution, and timing.

Sequential Execution Version

This version processes the jobs sequentially, one after the other.

package main

import (
    "fmt"
    "time"
)

func worker(id int, job int) string {
    time.Sleep(time.Second) // Simulate work
    return fmt.Sprintf("Worker %d completed job %d", id, job)
}

func main() {
    start := time.Now()
    results := make([]string, 5)

    for j := 1; j <= 5; j++ {
        results[j-1] = worker(1, j) // Call the worker function directly
    }

    for _, result := range results {
        fmt.Println(result)
    }

    duration := time.Since(start)
    fmt.Printf("It took %s to execute!", duration)
}
登录后复制

Output:

Worker 1 completed job 1
Worker 1 completed job 2
Worker 1 completed job 3
Worker 1 completed job 4
Worker 1 completed job 5
It took 5.048703s to execute!
登录后复制

Concurrent Execution Version

This version processes the jobs concurrently using goroutines and channels.

package main

import (
    "fmt"
    "time"
)

func worker(id int, jobs <-chan int, results chan<- string) {
    for job := range jobs {
        time.Sleep(time.Second) // Simulate work
        results <- fmt.Sprintf("Worker %d completed job %d", id, job)
    }
}

func main() {
    start := time.Now()
    jobs := make(chan int, 5)
    results := make(chan string)

    for w := 1; w <= 3; w++ {
        go worker(w, jobs, results)
    }

    for j := 1; j <= 5; j++ {
        jobs <- j
    }
    close(jobs)

    for a := 1; a <= 5; a++ {
        fmt.Println(<-results)
    }

    duration := time.Since(start)
    fmt.Printf("It took %s to execute!", duration)
}
登录后复制

Output:

Worker 1 completed job 1
Worker 2 completed job 2
Worker 3 completed job 3
Worker 1 completed job 4
Worker 2 completed job 5
It took 2.0227664s to execute!
登录后复制

Comparison

Structure:

  • Sequential Version: Calls the worker function directly in a loop. No concurrency.
  • Concurrent Version: Uses goroutines to run multiple worker functions concurrently and channels for job distribution and result collection.

Execution:

  • Sequential Version: Each job is processed one after the other, taking 1 second per job, leading to a total execution time roughly equal to the number of jobs (5 seconds for 5 jobs).
  • Concurrent Version: Multiple workers (3 in this case) process jobs concurrently, reducing the total execution time significantly. Jobs are distributed among the workers, and results are collected via channels.

Timing:

  • Sequential Version: Took approximately 5.048703 seconds.
  • Concurrent Version: Took approximately 2.0227664 seconds.

The concurrent version is significantly faster because it leverages parallel execution, allowing multiple jobs to be processed simultaneously. This reduces the total execution time to around the time it takes to complete the longest job, divided by the number of workers, rather than summing up the time for each job as in the sequential version.

Official Documentation References

  1. Go Documentation - Goroutines

    Goroutines

  2. Go Documentation - Channels

    Channels

  3. Go Blog - Concurrency in Go

    Concurrency in Go

  4. Go Documentation - The select Statement

    Select Statement

  5. Go Tour - Channels

    Tour of Go: Channels

Conclusion

In summary, the article provides a clear and simplified overview of channels in Go, emphasizing their role in facilitating safe communication between goroutines. By explaining the concepts of unbuffered and buffered channels, the article highlights their distinct behaviors and appropriate use cases. Additionally, it underscores the importance of closing channels to prevent deadlocks and ensure efficient resource management. With practical code examples and relatable analogies, the article equips readers with a foundational understanding of how to effectively utilize channels in their Go applications, paving the way for more robust concurrent programming.

以上是过于简化的 Golang 通道!的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!