Saluran Golang yang terlalu dipermudahkan!

WBOY
Lepaskan: 2024-07-28 14:07:13
asal
938 orang telah melayarinya

Oversimplified Golang Channel!

TL;DR

Artikel tersebut menerangkan saluran Go, yang membolehkan komunikasi selamat antara gorouti. Ia merangkumi cara membuat, menghantar dan menerima data melalui saluran, membezakan antara jenis tidak buffer dan jenis buffer. Ia menekankan kepentingan menutup saluran untuk mengelakkan kebuntuan dan menambah baik pengurusan sumber. Akhir sekali, ia memperkenalkan penyataan pilih untuk menguruskan operasi berbilang saluran dengan cekap.


Isi kandungan

  1. Pengenalan kepada Go Channels
  2. Membuat Saluran
  3. Menghantar Data
  4. Menerima Data
  5. Jenis Saluran dalam Go
    • Saluran Tidak Dibuffer
    • Saluran Penampan
  6. Menutup Saluran
    • Kenapa Tutup Saluran?
  7. Snippet Kod Tanpa Menutup Saluran
    • Output dan Ralat Dijangkakan
  8. Snippet Kod Dengan Menutup Saluran
    • Output Jangkaan
  9. Menggunakan Penyata pilihan
    • Contoh pilihan dengan Saluran
  10. Soalan Lazim tentang pilihan
  11. Memastikan Mesej daripada Kedua-dua Saluran Menggunakan WaitGroup
    • Contoh penggunaan WaitGroup
  12. Kesimpulan

Pengenalan kepada Saluran Go

Go, atau Golang, ialah bahasa pengaturcaraan berkuasa yang direka untuk kesederhanaan dan kecekapan. Salah satu ciri menonjolnya ialah konsep saluran, yang memudahkan komunikasi antara goroutine. Saluran membenarkan pertukaran data dan penyegerakan yang selamat, menjadikan pengaturcaraan serentak lebih mudah dan lebih terurus.

Dalam artikel ini, kami akan meneroka saluran dalam Go, memecahkan penciptaan, penghantaran data dan penerimaannya. Ini akan membantu anda memahami cara memanfaatkan saluran dengan berkesan dalam aplikasi anda.

Mencipta Saluran

Untuk membuat saluran dalam Go, anda menggunakan fungsi make. Berikut ialah coretan kod ringkas yang menunjukkan cara membuat saluran:

package main

import "fmt"

func main() {
    // Create a channel of type int
    ch := make(chan int)
    fmt.Println("Channel created:", ch)
}
Salin selepas log masuk

Dalam contoh ini, kami mencipta saluran ch yang boleh menghantar dan menerima integer. Saluran tidak ditimbal secara lalai, bermakna ia akan menyekat sehingga kedua-dua pengirim dan penerima bersedia.

Apabila anda menjalankan kod Go yang disediakan, output akan kelihatan seperti ini:

Channel created: 0xc000102060
Salin selepas log masuk

Penjelasan

  1. Penciptaan Saluran:

    • Barisan ch := make(chan int) mencipta saluran baharu jenis int. Saluran ini boleh digunakan untuk menghantar dan menerima nilai integer.
  2. Alamat Saluran:

    • Output 0xc000102060 ialah alamat memori saluran. Dalam Go, apabila anda mencetak saluran, ia memaparkan perwakilan dalamannya, yang termasuk alamatnya dalam ingatan.
    • Alamat ini menunjukkan tempat saluran disimpan dalam ingatan, tetapi ia tidak memberikan sebarang maklumat tentang keadaan atau kandungan saluran.

Menghantar Data

Setelah saluran dibuat, anda boleh menghantar data ke dalamnya menggunakan operator <-. Begini cara anda boleh menghantar data ke saluran:

go func() {
    ch <- 42 // Sending the value 42 to the channel
}()
Salin selepas log masuk

Dalam coretan ini, kami memulakan goroutine baharu yang menghantar nilai integer 42 ke saluran ch. Operasi tak segerak ini membolehkan atur cara utama terus melaksanakan semasa nilai dihantar.

Menerima Data

Untuk menerima data daripada saluran, anda juga menggunakan operator <-. Begini cara membaca dari saluran:

value := <-ch // Receiving data from the channel
fmt.Println("Received value:", value)
Salin selepas log masuk

Dalam contoh ini, kita membaca dari saluran ch dan menyimpan nilai yang diterima dalam nilai pembolehubah. Atur cara akan menyekat pada baris ini sehingga nilai tersedia untuk dibaca.

Jenis Saluran dalam Go

Dalam Go, saluran boleh dikategorikan terutamanya kepada dua jenis: saluran tidak buffer dan saluran buffer. Memahami jenis ini adalah penting untuk pengaturcaraan serentak yang berkesan.

1. Saluran Tidak Dibuffer

Saluran tanpa buffer adalah jenis yang paling mudah. Ia tidak mempunyai sebarang kapasiti untuk menyimpan data; ia memerlukan kedua-dua penghantar dan penerima bersedia pada masa yang sama.

Ciri-ciri:

  • Tingkah Laku Menyekat: Sekat operasi menghantar dan menerima sehingga kedua-dua pihak bersedia. Ini memastikan penyegerakan antara goroutine.
  • Kes Penggunaan: Terbaik untuk senario di mana anda mahukan penyegerakan yang ketat atau apabila komunikasi jarang berlaku.

Contoh:

ch := make(chan int) // Unbuffered channel
go func() {
    ch <- 1 // Sends data; blocks until received
}()
value := <-ch // Receives data; blocks until sent
fmt.Println("Received:", value)
Salin selepas log masuk

2. Saluran Penampan

Saluran buffer membolehkan anda menentukan kapasiti, bermakna ia boleh menyimpan bilangan nilai yang terhad sebelum menyekat penghantaran.

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.")
Salin selepas log masuk

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)
    }
}
Salin selepas log masuk

Expected Output and Error

fatal error: all goroutines are asleep - deadlock!
Salin selepas log masuk

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)
    }
}
Salin selepas log masuk

Expected Output

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

Message 1
Message 2
Message 3
Salin selepas log masuk

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)
    }
}
Salin selepas log masuk

Output with select:

Result from channel 1
Salin selepas log masuk

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)
    }
}
Salin selepas log masuk

Output

Result from channel 1
Result from channel 2
Salin selepas log masuk

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)
}
Salin selepas log masuk

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!
Salin selepas log masuk

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)
}
Salin selepas log masuk

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!
Salin selepas log masuk

Comparison

Structure:

  • 순차 버전: 루프에서 작업자 함수를 직접 호출합니다. 동시성이 없습니다.
  • 동시 버전: 고루틴을 사용하여 여러 작업자 기능을 동시에 실행하고 작업 배포 및 결과 수집을 위한 채널을 실행합니다.

실행:

  • 순차 버전: 각 작업은 작업당 1초가 걸리며 차례로 처리되므로 총 실행 시간은 대략 작업 수(5개 작업의 경우 5초)와 같습니다.
  • 동시 버전: 여러 작업자(이 경우 3명)가 작업을 동시에 처리하여 총 실행 시간을 크게 줄입니다. 작업은 작업자에게 분배되고 결과는 채널을 통해 수집됩니다.

타이밍:

  • 순차 버전: 약 5.048703초가 소요되었습니다.
  • 동시 버전: 약 2.0227664초가 소요되었습니다.

동시 버전은 병렬 실행을 활용하여 여러 작업을 동시에 처리할 수 있으므로 훨씬 더 빠릅니다. 이렇게 하면 순차 버전에서처럼 각 작업에 대한 시간을 합산하는 것이 아니라 가장 긴 작업을 완료하는 데 걸리는 시간을 작업자 수로 나눈 정도로 총 실행 시간이 줄어듭니다.

공식 문서 참조

  1. Go 문서 - 고루틴

    고루틴

  2. Go 문서 - 채널

    채널

  3. Go 블로그 - Go의 동시성

    Go의 동시성

  4. Go 문서 - select 문

    선택문

  5. 투어하기 - 채널

    Go 투어: 채널

결론

요약하자면, 이 기사는 Go의 채널에 대한 명확하고 단순화된 개요를 제공하며 고루틴 간의 안전한 통신을 촉진하는 채널의 역할을 강조합니다. 버퍼링되지 않은 채널과 버퍼링된 채널의 개념을 설명함으로써 이 기사에서는 고유한 동작과 적절한 사용 사례를 강조합니다. 또한 교착 상태를 방지하고 효율적인 리소스 관리를 보장하기 위해 채널을 닫는 것의 중요성을 강조합니다. 실용적인 코드 예제와 관련성 있는 비유를 통해 이 기사는 독자에게 Go 애플리케이션에서 채널을 효과적으로 활용하는 방법에 대한 기본적인 이해를 제공하여 보다 강력한 동시 프로그래밍을 위한 길을 닦습니다.

Atas ialah kandungan terperinci Saluran Golang yang terlalu dipermudahkan!. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

sumber:dev.to
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!