Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
dTub
Скачать

Understanding Go Concurrency: Why Your Results Differ from the Book Example

Автор: vlogize

Загружено: 2025-05-27

Просмотров: 1

Описание:

Discover why your Go program's concurrency results differ from the example in "Concurrency in Go" and learn how goroutine scheduling plays a crucial role.
---
This video is based on the question https://stackoverflow.com/q/66317906/ asked by the user 'Shid' ( https://stackoverflow.com/u/6112276/ ) and on the answer https://stackoverflow.com/a/66317950/ provided by the user 'Burak Serdar' ( https://stackoverflow.com/u/11923999/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Unable to get same results than in concurrency in Go book sample code

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding Go Concurrency: Why Your Results Differ from the Book Example

If you're diving into the world of concurrency in Go, you may have come across code examples that produce unexpected results when you try to run them. This often leads to confusion, especially when you're following along with a book, like Concurrency in Go: Tools and Techniques for Developers by Katherine Cox-Buday. In this post, we'll explore a common issue you might face if you're testing code from this book and provide you with a clear understanding of why your results may differ from what's expected.

The Problem

A reader of Concurrency in Go reported that the output of their code didn't match the results shown in the book. The task involved incrementing and decrementing a shared counter using goroutines and a mutex for synchronization. Despite following the code provided, their outputs varied each time they ran the program. Let's take a look at the code example and the outputs:

The Code

[[See Video to Reveal this Text or Code Snippet]]

Your Output

When you ran the code, you received the following output:

[[See Video to Reveal this Text or Code Snippet]]

Expected Output from the Book

The expected output listed in the book is quite different:

[[See Video to Reveal this Text or Code Snippet]]

Why the Differences Occur

Understanding Goroutines

The key factor to understand here is that when using goroutines in Go, there are no guarantees on the order in which they execute. The Go runtime decides the scheduling of these goroutines, leading to different possible interleavings of their execution. This means that:

Each time you run your program, the scheduling of goroutines may differ.

You may see more increments or decrements executed earlier or later than anticipated.

Valid Interleavings

The output you received is just as valid as the one presented in the book. Each run can yield various results based on how the Go runtime handles the scheduling of the goroutines. The difference is in the order and timing of their execution, which results in different outputs being printed.

Conclusion

When working with concurrency in Go, it's essential to embrace the concept of non-deterministic execution. The behavior you're observing with your program is an expected outcome when utilizing goroutines. The differences in outputs arise from the unpredictable nature of concurrency in Go, emphasizing the need to understand and design your code with these principles in mind.

By keeping this in mind, you'll be better equipped to troubleshoot issues related to concurrency and create more robust Go applications.

Keep experimenting and learning; concurrency is a nuanced topic that can significantly enhance your programming skills!

Understanding Go Concurrency: Why Your Results Differ from the Book Example

Поделиться в:

Доступные форматы для скачивания:

Скачать видео mp4

  • Информация по загрузке:

Скачать аудио mp3

Похожие видео

array(10) { [0]=> object(stdClass)#4358 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "UEU4SzBjqrc" ["related_video_title"]=> string(37) "HARD truths before switching to Go..." ["posted_time"]=> string(25) "2 месяца назад" ["channelName"]=> string(7) "Awesome" } [1]=> object(stdClass)#4331 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "wELNUHb3kuA" ["related_video_title"]=> string(78) "Master Go Programming With These Concurrency Patterns | Part 2 (in 40 minutes)" ["posted_time"]=> string(19) "1 год назад" ["channelName"]=> string(13) "Kantan Coding" } [2]=> object(stdClass)#4356 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "LvgVSSpwND8" ["related_video_title"]=> string(17) "Concurrency in Go" ["posted_time"]=> string(19) "7 лет назад" ["channelName"]=> string(11) "Jake Wright" } [3]=> object(stdClass)#4363 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "V-0ifUKCkBI" ["related_video_title"]=> string(36) "Concurrency and goroutines in golang" ["posted_time"]=> string(21) "3 года назад" ["channelName"]=> string(16) "Hitesh Choudhary" } [4]=> object(stdClass)#4342 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "rQQcIGqp0OU" ["related_video_title"]=> string(48) "The One BIG Reason to Learn Google's Go Language" ["posted_time"]=> string(21) "2 года назад" ["channelName"]=> string(13) "Gary Explains" } [5]=> object(stdClass)#4360 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "7DoP1L9nAAs" ["related_video_title"]=> string(54) "Scheduling Algorithms - First Come First Served (FCFS)" ["posted_time"]=> string(19) "5 лет назад" ["channelName"]=> string(12) "Neso Academy" } [6]=> object(stdClass)#4355 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "blWdjRUPP6E" ["related_video_title"]=> string(72) "Разведчик о том, как использовать людей" ["posted_time"]=> string(25) "4 недели назад" ["channelName"]=> string(18) "Коллектив" } [7]=> object(stdClass)#4365 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "kCey8Srxx7c" ["related_video_title"]=> string(93) "Новейшая ракета РФ уничтожена / Жаркая ночь в Крыму" ["posted_time"]=> string(21) "4 часа назад" ["channelName"]=> string(10) "NEXTA Live" } [8]=> object(stdClass)#4341 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "h9WbV-1YPF0" ["related_video_title"]=> string(127) "Вебинар GRASP-паттерны: Введение в шаблоны и путь от хаоса к архитектуре" ["posted_time"]=> string(63) "Трансляция закончилась 2 дня назад" ["channelName"]=> string(18) "Sergey Nemchinskiy" } [9]=> object(stdClass)#4359 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "W7KPfg0HSj8" ["related_video_title"]=> string(161) "Top 50 SHAZAM⛄Лучшая Музыка 2025⛄Зарубежные песни Хиты⛄Популярные Песни Слушать Бесплатно #46" ["posted_time"]=> string(23) "1 месяц назад" ["channelName"]=> string(9) "Open Deep" } }
HARD truths before switching to Go...

HARD truths before switching to Go...

Master Go Programming With These Concurrency Patterns | Part 2 (in 40 minutes)

Master Go Programming With These Concurrency Patterns | Part 2 (in 40 minutes)

Concurrency in Go

Concurrency in Go

Concurrency and goroutines in golang

Concurrency and goroutines in golang

The One BIG Reason to Learn Google's Go Language

The One BIG Reason to Learn Google's Go Language

Scheduling Algorithms - First Come First Served (FCFS)

Scheduling Algorithms - First Come First Served (FCFS)

Разведчик о том, как использовать людей

Разведчик о том, как использовать людей

Новейшая ракета РФ уничтожена / Жаркая ночь в Крыму

Новейшая ракета РФ уничтожена / Жаркая ночь в Крыму

Вебинар GRASP-паттерны: Введение в шаблоны и путь от хаоса к архитектуре

Вебинар GRASP-паттерны: Введение в шаблоны и путь от хаоса к архитектуре

Top 50 SHAZAM⛄Лучшая Музыка 2025⛄Зарубежные песни Хиты⛄Популярные Песни Слушать Бесплатно #46

Top 50 SHAZAM⛄Лучшая Музыка 2025⛄Зарубежные песни Хиты⛄Популярные Песни Слушать Бесплатно #46

© 2025 dtub. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]