Home > Backend Development > Golang > Why Does This Go Program Print 'ping' Then 'hello' Despite Concurrent Channel Writes?

Why Does This Go Program Print 'ping' Then 'hello' Despite Concurrent Channel Writes?

Barbara Streisand
Release: 2024-12-16 05:16:14
Original
743 people have browsed it

Why Does This Go Program Print

Understanding Channel Output Order in Golang

Consider the following Golang program:

func main() {
  messages := make(chan string)
  go func() { messages <- "hello" }()
  go func() { messages <- "ping" }()
  msg := <-messages
  msg2 := <-messages
  fmt.Println(msg)
  fmt.Println(msg2)
}
Copy after login

This program involves two goroutines asynchronously writing to a channel and the main routine reading from the same channel. Despite the unbuffered nature of the channel, the output consistently prints "ping" and then "hello."

To understand the reasoning behind this output, it's crucial to grasp that channel output order is not based on the order in which the goroutines are created. Instead, it's determined by the scheduler, which decides on the execution order of goroutines in a non-deterministic manner.

When the program executes, the two goroutines responsible for sending messages to the channel run concurrently. Since the channel is unbuffered, both goroutines block until a receiver becomes available.

When the main routine attempts to read from the channel using msg := <-messages, the scheduler grants access to one of the waiting goroutines. This goroutine successfully sends a message to the channel, which is then received by the main routine and assigned to msg.

Subsequently, when the main routine tries to read from the channel again using msg2 := <-messages, the scheduler selects the remaining goroutine and allows it to send its message to the channel. The message is then retrieved by the main routine and assigned to msg2.

In this particular case, the scheduler consistently selects the goroutine sending "ping" as the first to send its message. However, it's important to note that this outcome is non-deterministic and can change based on factors such as system load and execution environment.

To summarize, the output order in Golang channels is not guaranteed and is subject to the scheduler's execution order for goroutines. Hence, despite the simplicity of this example, the output may vary on different executions.

The above is the detailed content of Why Does This Go Program Print 'ping' Then 'hello' Despite Concurrent Channel Writes?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template