Go Concurrency Explained: Go Routines & Channels

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ย. 2024

ความคิดเห็น •

  • @MelkeyDev
    @MelkeyDev  ปีที่แล้ว +24

    I hope you all enjoyed this GO LANG video!
    Your comments mean the world to me. We are SO CLOSE to 5k subs!

    • @agustingomez7172
      @agustingomez7172 2 หลายเดือนก่อน

      nice video, can I use it in my page in a future as an example of the difference between normal process vs using go routine and channels?

  • @brunomello7499
    @brunomello7499 29 วันที่ผ่านมา +5

    Just a tip: instead of modifying the fetch function to use channels and go routines, should probably just encapsulate the function call inside ankther function and handle channel communication from there
    go func() {
    fetch(...)
    ...
    }
    This way you don't change the fetch function behaviour and it can be used both with and without go routines

  • @ohLyln
    @ohLyln ปีที่แล้ว +23

    Great video for those unfamiliar with goroutines! One way to estimate how much faster this will make your code is like this: the first way of running it just one after another in a loop will take the time to call a + the time to call b + time to call c etc. When using goroutines, these are all running at the same time, however you're waiting for the collection of them to finish (using wg.Wait()). This means you will be held back by the *slowest* single call whether its a or b or c etc.

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +2

      Thanks for the info!

    • @guhkunpatata3150
      @guhkunpatata3150 ปีที่แล้ว +1

      great insight. is it safe to assume that it is an issue ? if yes, is there any way to overcome this issue?

    • @ohLyln
      @ohLyln ปีที่แล้ว

      @@guhkunpatata3150 no its not an issue at all. It's the whole point of using goroutines. They allow you to take something that say runs 5 times each x amount of seconds and go from 5x to just 1x of time.

    • @LunodzoMwinuka
      @LunodzoMwinuka 2 หลายเดือนก่อน

      This is insightful. Are there better alternatives?

  • @VikingPingvin93
    @VikingPingvin93 ปีที่แล้ว +43

    Great video.
    Just mentioning that the speed gain is much more visible if you don't only test for 4 API calls, but for a 100 or 1000.

  • @Nintron
    @Nintron ปีที่แล้ว +34

    2:06 I'm so used to Prime screaming TOKKKIIOOOOO I half expected Melkey to do that here...

  • @headlights-go-up
    @headlights-go-up ปีที่แล้ว +22

    I seriously love and value these videos about Go's main features.

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +1

      I should do more then??

    • @kkebo
      @kkebo ปีที่แล้ว

      @@MelkeyDev Definitely please! Really useful for someone who is currently learning Go like me

    • @headlights-go-up
      @headlights-go-up ปีที่แล้ว

      @@MelkeyDev yes please, mi lord

  • @yooos3
    @yooos3 6 หลายเดือนก่อน +1

    Amazing! I recently joined a new company and right now I see the code written to process 1.3G of json data is done with single thread in Java! We are switching to golang and migrating our etl process too, go routines will definitely cut short the processing time from 8 hours to ~2 hours or less. Already did some POC and just like you mentioned, I am seeing more than 3 times better performance in prod environment!

  • @hanzofuma
    @hanzofuma ปีที่แล้ว +8

    Today I had to make an application that parses a tsv file with 25k+ lines and make some requests to get other data using apis over an interval of time and caches the results. as a newbie to Go I thought it's time to learn goroutines and then I opened Firefox going to Go tour I found your video In the 1st Row lol what a timing. Thanks for the video 😁.

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +1

      Hell yeah my pleasure

  • @ed223-p5g
    @ed223-p5g ปีที่แล้ว +12

    why do you execute the wg.Wait and the channel close in a goroutine?

  • @BbB-vr9uh
    @BbB-vr9uh 8 หลายเดือนก่อน +1

    Came to learn to do exactly this. Wanted to call the same api endpoint for multiple inputs and not wait for them all sequentially.

    • @MelkeyDev
      @MelkeyDev  8 หลายเดือนก่อน

      Hopefully this was helpful!

  • @adobko
    @adobko 6 หลายเดือนก่อน

    I am a total novice in Go, but watching this video showd me so much cool grifs , forexample that anonymous function to close the wait group, or the fact of being abel to put struct into struct, it was awesome, thanks.

  • @AsToNlele
    @AsToNlele ปีที่แล้ว +3

    My favourite part was when Go said it's Going time and went way too fast
    Great video

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +1

      Glad you liked it!

  • @zeroxthree
    @zeroxthree ปีที่แล้ว +3

    7:37, dude i scared me

  • @Bleedpool
    @Bleedpool ปีที่แล้ว +1

    very quickly and simply explained Goroutines, appreciate it!

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      Glad it was helpful!

  • @jyuart
    @jyuart 11 หลายเดือนก่อน +2

    I’ve heard (and maybe I misunderstood the point) that one of the main advantages of go routines over other languages utilizing async is that you don’t have to rewrite your functions. You just add go at the beginning at that’s it. But from what I’m seeing it’s applicable only to functions that do not return anything. If you function returns any value, any you want to run it with go, you’d need to add a channel as an argument to be able to pass the return value back to the caller. Is that correct?

    • @MelkeyDev
      @MelkeyDev  11 หลายเดือนก่อน

      The only way to communicate between GO routines is through channels

  • @anarbekomurzakov8089
    @anarbekomurzakov8089 หลายเดือนก่อน

    Hey, Melkey this is really great one, enjoyed it! I wonder it would be great if one day you could build a complete demo project with goroutines 🙂

  • @erkropka
    @erkropka ปีที่แล้ว +3

    Sorry, I do not know if someone has already mentioned it but in the second case you do not print the temp of the city but just the name of the city ;) I hope this is fixed in the code example if you are sharing it.

    • @deniswarsidin9402
      @deniswarsidin9402 5 หลายเดือนก่อน

      Most important thing the return data in weather function was useless.

  • @caballerosalas
    @caballerosalas 9 หลายเดือนก่อน +2

    The video starts at 1:28

  • @emil_l889
    @emil_l889 ปีที่แล้ว +3

    WE WANT MORE

  • @shoxruhxonismoilov-nw8zb
    @shoxruhxonismoilov-nw8zb 8 หลายเดือนก่อน

    Great video, I'd like to ask you to do more big projects in the Go programming language. This would be a great experience for language learners !

  • @axMf3qTI
    @axMf3qTI 9 หลายเดือนก่อน +2

    So what does the wait group and channels do? If that only needed if you return something from the function. When I look up go routines I get an example with the go keyword but no mention of wait group or channels.

    • @mushroom_noodles
      @mushroom_noodles 8 หลายเดือนก่อน

      goroutines communicate between each other using channels. this way we can send and receive data from different goroutines running concurrent or parallel. when it comes to wait group, it is usually used to synchronise all goroutines since some of them may execute faster than another. for example, when running a program with go funcs() in it, main function may execute faster than all the goroutines and you will simply end up doing nothing. you can fix this by using sync.WaitGroup{} so the main function will wait for the group to execute before shutting down the application. those are also used to prevent deadlocks in your app

    • @konradpiotrowski9549
      @konradpiotrowski9549 6 หลายเดือนก่อน

      Basically in his example it is used only for making sure when to close the channel. Not closing might (but not necessarily) lead to data leak

  • @fliplucky8813
    @fliplucky8813 ปีที่แล้ว +4

    - saw this video
    - was working
    ....
    Alright, time for a break and watch MelkyDev

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      Its melkey time baybay

  • @ethaaan467
    @ethaaan467 9 หลายเดือนก่อน

    simple and on point. great videos bro!

  • @johnjohnes698
    @johnjohnes698 9 หลายเดือนก่อน +7

    it would be nice if you brought a bit less of drama, you are overplaying

    • @MelkeyDev
      @MelkeyDev  9 หลายเดือนก่อน +1

      Did you put this in the correct video?

  • @mamg301988
    @mamg301988 ปีที่แล้ว

    great video, and What keyboard do you have?

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      I use the Moonlander

  • @SniboyPL
    @SniboyPL ปีที่แล้ว +6

    "concept of executing multiple tasks simultaenously by utilizing all available resources more effectively" - isn't this parallelization and not concurrency?

  • @laureal1699
    @laureal1699 8 หลายเดือนก่อน +1

    where can I review the full code bro?

  • @developertools95
    @developertools95 หลายเดือนก่อน

    is that same of eventloop concept in js?

  • @hebozhe
    @hebozhe ปีที่แล้ว +1

    You may want to capture common errors that don't get caught as errors, like leaving routines asleep, negative allocation, and deadlocks.

  • @vaster1142
    @vaster1142 ปีที่แล้ว

    I'm subscribing.

  • @dwdtalks
    @dwdtalks 11 หลายเดือนก่อน

    Amazing

  • @astrocanyounaut
    @astrocanyounaut ปีที่แล้ว +1

    god dang- mans is fr making me wanna switch to go from rust 😖 awesome vid!!! 2x a week bayBEE 🔥

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +1

      Bruh RUST is SO old

  • @manfrombritain6816
    @manfrombritain6816 9 หลายเดือนก่อน +1

    what is the purpose of the anonymous func that tells the wg to wait, and to then close the channel? doesn't it work the same without the func?

    • @maryembenyounes523
      @maryembenyounes523 5 หลายเดือนก่อน

      i guess so that it doesn't block the main function because we need to print the results as they enter the channel

    • @cyrilc189
      @cyrilc189 5 หลายเดือนก่อน

      It seems to defeat the purpose of the wait group which is to ensure synchronization. Synchronizing in a separate go routine can produce unpredictable results.

  • @_thisisnotjohn
    @_thisisnotjohn ปีที่แล้ว

    Gj 👏👏 What about making a tutorial where you build a simple cli tool in GO?

  • @dixon2233
    @dixon2233 9 หลายเดือนก่อน

    He's not wrong when he started off off with "We all like go" lmao

  • @ameerhamza4816
    @ameerhamza4816 8 หลายเดือนก่อน

    entertaining and informative thanks

    • @MelkeyDev
      @MelkeyDev  8 หลายเดือนก่อน

      Glad you enjoyed it

  • @lawniczakjohn
    @lawniczakjohn ปีที่แล้ว +1

    Sweet video my dude! Got a link to that tasty concurrency fire file?

  • @codelinx
    @codelinx ปีที่แล้ว

    what i would like to see is advanced looping structures, more specifically related to algos or solving algos using some of these crazy loops and 2/3 pointer techniques with for loops.

  • @DeviantFox
    @DeviantFox 7 หลายเดือนก่อน

    can you limit how many resources you use instead of all or nothing?

  • @abdurahmanmohamed4732
    @abdurahmanmohamed4732 3 หลายเดือนก่อน

    Consider keeping the code zoomed in, it's really hard to follow this way, and the recycling between the zoom in and out is causing me headache.

  • @SanFranciscoCyclist
    @SanFranciscoCyclist ปีที่แล้ว

    Hey even for basic examples, posting a Github link to your working example would be 🔥. Thanks for the video.

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      Thanks for the idea!

  • @bumpy_lumps
    @bumpy_lumps ปีที่แล้ว +1

    WE LOVE GO

  • @zoeyfryhover1816
    @zoeyfryhover1816 5 หลายเดือนก่อน +1

    You stopped returning the data and just returned the names of the cities you passed in?

  • @JaffyMaglinte
    @JaffyMaglinte 6 หลายเดือนก่อน

    Thank you!!!

  • @konradpiotrowski9549
    @konradpiotrowski9549 6 หลายเดือนก่อน

    I am not sure if your example with reading the data from huge csv file using goroutines will make it any faster. At this point you are limited by your CPU power, if you create a few goroutines, CPU power will be split amongst them. It is nice to spawn a goroutine in order to not block completely your app, so you can read csv file in the background but sorry, unless you give me a code example I am not convinced about that

    • @MelkeyDev
      @MelkeyDev  6 หลายเดือนก่อน

      It can set you up to parallelize the tasks if you have more than 1 CPU

  • @derrique21
    @derrique21 22 วันที่ผ่านมา

    Seeing 2 different terminal outputs for each implementation 6:31
    NON go routine:
    This is the data {{292.9}}
    This is the data{{288.84}}

    go routine:
    This is the Paris
    This is the London

    Is this expected? Not seeing the data from get request..

    • @derrique21
      @derrique21 22 วันที่ผ่านมา

      Ignore, I didn’t scroll down far enough. Looks like this was already mentioned

  • @Mr.Robots
    @Mr.Robots 10 หลายเดือนก่อน +1

    Tks for the video! But the concurrency definition is not that.

  • @hardikvegad3508
    @hardikvegad3508 7 หลายเดือนก่อน

    why dont u make a course on go, it will be lot helpful. Thanks!

  • @YerbolAssylhanov
    @YerbolAssylhanov 8 หลายเดือนก่อน

    cool video)

  • @AlejandroMéndez-j6j
    @AlejandroMéndez-j6j 16 วันที่ผ่านมา

    Hard on the selection bias of that sample of size 3...

  • @joaodiasconde
    @joaodiasconde ปีที่แล้ว +2

    Go's concurrency is awesome, literally the main feature, makes up for some of the ugly sides of Go for sure. Liked the video, I would maybe just add a larger array of cities so the difference is more noticeable. Probably a multiple of the number of your logical cores, to see if it scaled down linearly.

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +4

      I think we need a part two then no?

    • @joaodiasconde
      @joaodiasconde ปีที่แล้ว

      @@MelkeyDev Thinking? That was your first mistake just GO ♿♿♿

    • @BboyKeny
      @BboyKeny ปีที่แล้ว +1

      I prefer Rust's error handling over that of Go, although Go's error as value is better than throwing and catching exceptions imho. But nothing beats Go's concurrency (maybe in performance but not in syntax)

    • @joaodiasconde
      @joaodiasconde ปีที่แล้ว

      @@BboyKeny 100%, rust's error handling is the best out there

  • @aloufin
    @aloufin ปีที่แล้ว +1

    ok the glitch sound effect at 7;38 is waaay to loud holy shit scared me Haah

  • @yoloopen
    @yoloopen ปีที่แล้ว +6

    "Concurrency is executing tasks at the same time", "good example if it is to read a bunch of rows" - that's insane, it's so well covered topic, it takes 10 seconds to google and briefly check a sum up and pick up some example, why didn't you do it? you kinda want to teach other people, but you don't care enough to even google?

  • @nijatSh
    @nijatSh 8 หลายเดือนก่อน +1

    I didn't see any effect of goroutines in this video😅

  • @carolli5960
    @carolli5960 3 หลายเดือนก่อน

    I think the data need to be sent to the chan and remove the return data in fetchWeather func. And it don't need the anonymous func

  • @crusader_
    @crusader_ ปีที่แล้ว

    Please make a video about error handling. Address the `if err != nil` issue

    • @spyroninja
      @spyroninja 8 หลายเดือนก่อน +1

      It's not an issue

    • @GusPm
      @GusPm 7 หลายเดือนก่อน

      It’s a feature bro

  • @aryamoghaddam529
    @aryamoghaddam529 10 หลายเดือนก่อน +54

    Concurrency is handling multiple tasks at once, NOT executing them. You can have one CPU core execute tasks in consecutive order or which their queue up. Please learn first then make videos Thanks

    • @neneodonkor
      @neneodonkor 8 หลายเดือนก่อน +5

      So handling multiple task AT ONCE IS WHAT?

    • @KevinLanguasco
      @KevinLanguasco 8 หลายเดือนก่อน +7

      ​@@neneodonkorParallelism

    • @viridianite
      @viridianite 6 หลายเดือนก่อน

      To piggyback on this, with a single CPU tasks run concurrently in any modern OS. A task is run for a bit, another task for a bit, and so on and forth. The thing is the CPU scheduler switches between tasks fairly often and quickly, which gives the impression the tasks are executed at the same time. However at any given point in time only a single task is running on a given CPU.
      When it comes to parallelism, first tasks must be broken doen to run in a concurrent fashion and then you must have the hardware to run them, i.e., tasks running at exact the same time in different CPUs. Without hardware support, i.e., more CPUs, you cannot have parallelism. Since tasks must be able to run concurrently if you want them to run in parallel (assuming you've enough CPUs), we can say that parallelism is a subset of concurrency. Thus, all parallel tasks are concurrent but not all concurrent tasks are parallel.
      Any good OS book goes over this. I personally recommend Ramzi's OS: Three Easy Pieces. For a more practical book, I'm finding Cutajar's "Learn Concurrent Programming with Go" quite good.

    • @viridianite
      @viridianite 6 หลายเดือนก่อน +4

      ​@@neneodonkorHANDLING multiple tasks AT ONCE is concurrency, EXECUTING multiple tasks AT ONCE is parallelism.
      Handling means that instead of executing a task to completion on a CPU, followed by another task, etc., the CPU scheduler runs a task for slices of time and thus interleaving their execution time.

    • @sklarenbach
      @sklarenbach 3 หลายเดือนก่อน +2

      Here are precise definitions of concurrency and parallelism in computer science:
      Concurrency:
      Concurrency refers to the ability of different parts or units of a program, algorithm, or system to be executed out-of-order or in partial order, without affecting the final outcome. It allows for multiple tasks to be in progress simultaneously, but not necessarily executing at the exact same time. Concurrent execution may involve interleaving the execution of multiple tasks on a single processor or core.
      Key points:
      - Tasks can start, run, and complete in overlapping time periods
      - Does not necessarily imply simultaneous execution
      - Focuses on dealing with multiple tasks at once
      Parallelism:
      Parallelism refers to the simultaneous execution of multiple tasks or processes at the same instant of time. It requires hardware with multiple processing units, such as multi-core processors or distributed systems. Parallel execution aims to divide a task into smaller subtasks that can be processed simultaneously, thus reducing the overall processing time.
      Key points:
      - Tasks are literally executed at the same time
      - Requires hardware support (multiple processors/cores)
      - Focuses on simultaneous execution to improve performance
      The main difference is that concurrency is about structure and dealing with multiple tasks, while parallelism is about execution and performing multiple tasks simultaneously.

  • @TMANandMAISON991
    @TMANandMAISON991 9 หลายเดือนก่อน +1

    Isn't that supposed to take 100ms after using goroutine?

    • @MelkeyDev
      @MelkeyDev  9 หลายเดือนก่อน

      Hm, not sure what you mean!

    • @TMANandMAISON991
      @TMANandMAISON991 4 หลายเดือนก่อน

      @@MelkeyDev if it takes ~400ms to complete 4 operations consecutively, then one request takes about ~100 ms
      so if we make 4 requests in parallel that should take ~100ms as well

  • @kkebo
    @kkebo ปีที่แล้ว +1

    Is powing Melkey's version of brofist

  • @lobokun
    @lobokun ปีที่แล้ว +1

    is there a place we can get the code?

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +1

      I think I should add it to the description

  • @GameSmilexD
    @GameSmilexD ปีที่แล้ว

    go is going to build a lot of tech in the future, its the easiest language to integrate with an LLM u just need to understand a bit of architecture and debugging and its still fast, python with jit to asm/c is the current most used option and imo the best but cant deny the facts

  • @solo-im1wl
    @solo-im1wl ปีที่แล้ว +1

    race conditions in the final run xd

  • @Muhammed-nani964
    @Muhammed-nani964 ปีที่แล้ว

    You should really create a short intro hmmm when will you be live ? It’s already the weekend

  • @emil_l889
    @emil_l889 ปีที่แล้ว

    MORE GO CONCURRENCY

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      ALL MORE WE WANT

  • @abdessalemmohellebi
    @abdessalemmohellebi 9 หลายเดือนก่อน +1

    Nah i'd go🗿

  • @trash_dev
    @trash_dev ปีที่แล้ว +2

    i dont like go

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      but you like me

  • @kylestubblefield3404
    @kylestubblefield3404 ปีที่แล้ว

    I am desperate to get wifi working for the esp32 board so I can use tinygo for my micro controllers

  • @ricardochi6637
    @ricardochi6637 ปีที่แล้ว

    Hi, my unique doubt is how we print the data from the api? i see that only printing the city.

  • @kubre
    @kubre ปีที่แล้ว +1

    Lets goooo

  • @larrystone654
    @larrystone654 ปีที่แล้ว +1

    2:51 it’s how hot?!

  • @kirillb.9322
    @kirillb.9322 5 หลายเดือนก่อน

    Don’t show him Event loop guys 😂

  • @choleralul
    @choleralul ปีที่แล้ว

    Screw benchmarks, tell us ho much you benchPRESS!

  • @maksimteplyy1404
    @maksimteplyy1404 8 หลายเดือนก่อน

    Niceee

  • @RobertMartin-z3z
    @RobertMartin-z3z หลายเดือนก่อน

    Kuvalis Alley

  • @ramessu7976
    @ramessu7976 หลายเดือนก่อน

    Not for high trading

  • @j.r.r.tolkien8724
    @j.r.r.tolkien8724 ปีที่แล้ว +2

    Congratulations! This is the worst explanation I've seen so far and I've seen a lot.

  • @Endurancecoach00
    @Endurancecoach00 ปีที่แล้ว +1

    Concurrenthy

  • @thewhiteoaktree
    @thewhiteoaktree ปีที่แล้ว

    Bro I just did a fat line of PHP

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว

      we need to get you help

  • @abdallahelhdad819
    @abdallahelhdad819 ปีที่แล้ว

    tbh i understand nothing.. i played with c++ modern and js stuff but still understands 0. looks like good content, but not for me

  • @hansmuster5291
    @hansmuster5291 9 หลายเดือนก่อน

    please put in some more effort next time... it does not even print the temperatures in the end 🙄

  • @WarrenBuffering-kj7us
    @WarrenBuffering-kj7us ปีที่แล้ว

    I genuinely don't understand this meme

  • @emil_l889
    @emil_l889 ปีที่แล้ว +2

    GOCURRENCY

    • @MelkeyDev
      @MelkeyDev  ปีที่แล้ว +1

      Call that Gorrency