Is this a race condition in Go?
php editor Apple will answer a common question for you in this article: "Is this a race condition in Go?" When writing concurrent programs, the race condition is A common problem that can lead to data inconsistencies and other unexpected results. In the Go language, we can use mechanisms such as mutex locks and channels to avoid race conditions. Let’s discuss it together!
Question content
func main() { m := map[string]int{ "foo": 42, "bar": 1337, } go func() { time.Sleep(1 * time.Second) tmp := map[string]int{ "foo": 44, "bar": 1339, } m = tmp }() for { val := m["foo"] fmt.Println(val) } }
I see this in a lot of packages.
Why isn't this considered a race condition?
go run -race .
No errors.
Solution
As @volker pointed out, this is a data race. And since it's only written once, it's difficult to detect. Here is a modified demo that can easily trigger data race errors:
package main import ( "fmt" "time" ) func main() { m := map[string]int{ "foo": 42, "bar": 1337, } done := make(chan any) go func() { for i := 0; i < 100; i++ { time.Sleep(time.Microsecond) tmp := map[string]int{ "foo": 44, "bar": 1339, } m = tmp } close(done) }() for { select { case <-done: return default: val := m["foo"] fmt.Println(val) } } }
The above is the detailed content of Is this a race condition in Go?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
