Тёмный

Why understanding concurrency in Go is so important 

Web Dev Cody
Подписаться 234 тыс.
Просмотров 10 тыс.
50% 1

Become a YT Members to get extra perks!
www.youtube.co...
My Products
🏗️ WDC StarterKit: wdcstarterkit.com
📖 ProjectPlannerAI: projectplanner...
🤖 IconGeneratorAI: icongeneratora...
Useful Links
💬 Discord: / discord
🔔 Newsletter: newsletter.web...
📁 GitHub: github.com/web...
📺 Twitch: / webdevcody
🤖 Website: webdevcody.com
🐦 Twitter: / webdevcody

Опубликовано:

 

10 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 80   
@clayton.schneider
@clayton.schneider Месяц назад
A big mindset shift for me was passing by pointer or by copy. Sometimes it’s simpler to just pass a copy of data and know that no other go routine can access that data.
@twitchizle
@twitchizle Месяц назад
Pass by pointer if its object, pass by value if its primitive.
@keshavakumar9828
@keshavakumar9828 Месяц назад
@@twitchizle pass by pointer if its concurrent, be it primitive or object
@romarioputra3775
@romarioputra3775 Месяц назад
yep, immutable data is thread safe
@DonDikaio
@DonDikaio Месяц назад
"Now that I'm a professional Go Developer" how can you not love the attitude.
@yousofscodingadventures
@yousofscodingadventures Месяц назад
Welcome to Multithreading. Mutexes are the worst and slowest way to do it but it's the easiest. You have scoped variables, semaphores, and atomic operations. It's a big world.
@massy-3961
@massy-3961 Месяц назад
Shared mutable data is a foot gun of a developer rather than the language, except rust I guess. It’s more of your duty to make sure you don’t have multiple write access to the same variable. Only reason JavaScript doesn’t have this issue is that it’s single threaded and will never have concurrent access. Also if you really want, there is a built in atomic package that has an atomic int that allows you to have shared access safely. Another option that you can do is just use channels as they thrive in concurrency. Make a channel that has a go routine that just listens and increments the real counter on receive, then all handlers can just read the count and use the channel to increment. Would be nice to wrap all of that in a struct.
@vladlazar94
@vladlazar94 22 дня назад
Exactly my thoughts, thanks for writing this. An important additional benefit of using channels is that they can buffer messages. A mutex blocks all coroutines waiting on it, whereas a buffered channel can be written to even when the select at its other end is not ready to process the message. So instead of waiting on the mutex, a coroutine pushes a message into the buffered channel and carries on with its work. This is called handling backpressure.
@hamm8934
@hamm8934 Месяц назад
I wouldnt say this is the “biggest” footgun. This is just a learning pain of going with a little lower level of a language. Struct properties being optional is probably a bigger footgun. It basically forces you to either check for nils on all props every time, or create contractors for all structs with arguments for all required properties. Everyone was asking for generics back in the day, but i still stand by this being the biggest weak point of Go. I find the constructor convention isnt too bad and gets the job done, it just seems like such an easy win for them to add a compiler config for never nil properties or to add an optional syntax of some kind.
@WebDevCody
@WebDevCody Месяц назад
it's a footgun, a powerful tool of the language you could easily screw yourself over with if not careful
@hamm8934
@hamm8934 Месяц назад
@@WebDevCody im not one for splitting hairs over definition pedantics, and i dont disagree with the havoc this could cause. However, i just dont think this is the “biggest” (as the original video title stated) weakness of Go. Sure, you might face this problem starting out if you’re new to concurrency or channels at a lower level, but once you learn them, these issues dont really come up frequently in practice. As you write more Go, whenever you see a go routine you just kinda build up a mental note to check for referencing something outside of the channel scope or check for needing a mutex. However, nil pointers and seg faults in Go come up quite often, especially when communicating with an external API or poorly crafted Go bindings. There’s a reason folks call null a “billion dollar mistake.” Having non-required struct properties is a far bigger footgun in Go in my perspective, having used Go as my primary backend language for coming up on 4 years. Nil pointers are just in your face constantly in Go, while incorrectly passing data between channels and the main thread or using a mutex isnt as frequent of a problem.
@WebDevCody
@WebDevCody Месяц назад
@@hamm8934 I went ahead and updated the title anyway 🤣 too many people didn’t like the word foot gun and I never said the word once in the video anyway. I haven’t used go long enough to know the true footguns, so I’ll just avoid the word
@hamm8934
@hamm8934 Месяц назад
@@WebDevCody fair :) great video none the less Hope you keep liking go and dont give up just yet on htmx and templ! Cheers
@WebDevCody
@WebDevCody Месяц назад
@@hamm8934 I’m trying to hang on but I’m missing nextjs already 😆
@k98killer
@k98killer 19 дней назад
Goroutines run concurrently and/or in parallel. I wrote and parallelized a genetic algorithm library in Go last year (and then optimized memory use with object pools). It basically takes a batch of tasks and divides them up into the number of goroutines passed as an argument, using channels more or less as task queues and locks on every re-used object. It runs with 100% CPU utilization on all 8 cores as long as I specify 8 or more goroutines. Concurrency is tricky.
@C4CH3S
@C4CH3S Месяц назад
Yes, this is why you should avoid side effects as much as possible in golang.
@lemurza5236
@lemurza5236 Месяц назад
I'm not sure i consider the a Go footgun?
@spencerwilson-softwaredeve6384
@spencerwilson-softwaredeve6384 Месяц назад
Basic concurrency handling, shared mutable state requires a mutex if multiple threads want to access it at once, I guess javascript/python just hides all of this away
@hamm8934
@hamm8934 Месяц назад
This is more so learning pains when going a little lower level
@WebDevCody
@WebDevCody Месяц назад
Got em
@Frostbytedigital
@Frostbytedigital Месяц назад
@lemurza5236 I think you can consider it a foot gun since go compiler doesn't flag variables accessed within go routines that aren't locked.
@perc-ai
@perc-ai Месяц назад
this is just a skill issue not a footgun by any means its designed perfectly
@SeibertSwirl
@SeibertSwirl Месяц назад
Ugh finally lol love ya babe! Great job as usual ❤
@WebDevCody
@WebDevCody Месяц назад
You’re the best!
@Yesquiteindeed321321
@Yesquiteindeed321321 Месяц назад
Must be nice to be a babe to someone :,) Ya’ll are goals
@Jesse-x8p
@Jesse-x8p Месяц назад
"...share memory by communicating " is something of a mantra among go devs. You should use channels when dealing with these kinds of situations as channels also synchronise(only unbuffered channels). It doesn't hurt performance as much as mutexes and they are concurrency safe(no race conditions or data races). Also, you should always tests ;)
@Frostbytedigital
@Frostbytedigital Месяц назад
Cody getting lots of love from prime lately. Wonder if this one will get picked up too.
@zendr0
@zendr0 Месяц назад
Good one Cody! Look forward for more
@erikslorenz
@erikslorenz Месяц назад
The number one one I always forget is in regular handlers doing the naked return after an early response. Then again I always forgot that in node too.
@tmanley1985
@tmanley1985 Месяц назад
This is also known as the Lost Updates problem in concurrency. You can use locks, but you can also use channels to ensure serialized access to some variable like a counter.
@khazixfangirl6087
@khazixfangirl6087 Месяц назад
For this little example, I think the incrementing will work if you make it atomic
@arturfil
@arturfil Месяц назад
Yeah this happens because the variable is trying to be updated by multiple threads at the same time and each thread doesn't have recount of the previous. A mutex (or a semaphor) only allows one thread to update the variable at a time and solves this problem. Channels is like a nicer semaphor version where the variable is also locked if the channel has no buffer or has 1 "unit" of buffer. Pretty cool stuff imo.
@EduarteBDO
@EduarteBDO Месяц назад
that is pretty interesting
@Simbysolo
@Simbysolo Месяц назад
Thanks for pointing this out
@random-Automation-23242
@random-Automation-23242 Месяц назад
is there a way to import node modules in k6?
@oumardicko5593
@oumardicko5593 Месяц назад
if you wrote test cases and run them concurrently and if your code is not concurrent safe, the compiler will throw an error
@dejanduh2645
@dejanduh2645 Месяц назад
For such example, you could show atomic values as a replacement for mutex
@meharjeetsingh5256
@meharjeetsingh5256 Месяц назад
Does go have any atomics to make things faster?
@BarakaAndrew
@BarakaAndrew Месяц назад
Yap, there's atomic package built in.
@abhishekmehandiratta4241
@abhishekmehandiratta4241 Месяц назад
If you're sharing primitives across goroutines, you can use the atomic package which I've heard is more performant.
@anatolio7975
@anatolio7975 Месяц назад
Could also use an atomic integer
@koustavmondal2458
@koustavmondal2458 Месяц назад
Can you please make a video on how did u setup tailwind+daisyui in ur go project !!??
@heheboy9863
@heheboy9863 Месяц назад
What name your Vs code theme?
@kyngcytro
@kyngcytro Месяц назад
Had the same experience with Python. Or at least it was close.
@BarakaAndrew
@BarakaAndrew Месяц назад
You are getting there, next up interfaces then always passing interfaces to structs, Go is done.
@muhammadzikri900
@muhammadzikri900 Месяц назад
theme vscode use ?
@banafish
@banafish Месяц назад
you've switched to maybe the ugliest vscode theme on all of youtube, but I'm still watching :)
@neociber24
@neociber24 Месяц назад
Multithreading is always a footgun except in Rust.
@notusperson4667
@notusperson4667 Месяц назад
Elaborate more please
@filipg4
@filipg4 Месяц назад
Yes, because in Rust it's a footcrab instead.
@baxiry.
@baxiry. Месяц назад
On the contrary. Rust apps are full of data race. Go apps are cleaner.
@OnFireByte
@OnFireByte Месяц назад
yeah you have to choose between footgun or arc
@Astrelune
@Astrelune Месяц назад
Rust cultist is everywhere
@ktappdev
@ktappdev Месяц назад
All respect to you sir, but I wouldn't call this a foot gun.
@WebDevCody
@WebDevCody Месяц назад
what would you call it?
@Heshamelfakharani
@Heshamelfakharani Месяц назад
are you using an ORM?
@WebDevCody
@WebDevCody Месяц назад
Nah just SQL directly
@EverydayElk
@EverydayElk Месяц назад
for one thing, don't use globals in Go
@meni181818
@meni181818 28 дней назад
atomic
@meni181818
@meni181818 28 дней назад
btw you are the best
@salman0ansari
@salman0ansari Месяц назад
channels are better approach to do this
@DanielBergholz
@DanielBergholz Месяц назад
Have you heard about our lord and savior Elixir? There is no race conditions there due to each process being 100% isolated from each other. That’s the power of a truly functional programming language
@SeibertSwirl
@SeibertSwirl Месяц назад
FIRRRSTTTT
@WebDevCody
@WebDevCody Месяц назад
Love you!
@niklavskarklins9663
@niklavskarklins9663 Месяц назад
The theme has to go..
Далее
Apple Event - September 9
1:38:50
Просмотров 25 млн
Linus Torvalds: Speaks on Hype and the Future of AI
9:02
Why Golang Developers HATE Gorm…
4:07
Просмотров 32 тыс.
Golang Channels Or Wait Groups? Let Me Explain.
18:32
Why HTMX and Golang? The answer might surprise you...
12:04
My 10 “Clean” Code Principles (Start These Now)
15:12
How To Use The Context Package In Golang?
17:03
Просмотров 60 тыс.
Concurrency in Go
18:40
Просмотров 615 тыс.
Apple Event - September 9
1:38:50
Просмотров 25 млн