How To Use Goroutines For Aggregating Data In Golang?!

Поделиться
HTML-код
  • Опубликовано: 4 фев 2025

Комментарии • 70

  • @anthonygg_
    @anthonygg_  2 года назад +2

    ► Join my Discord community for free education 👉 discord.com/invite/bDy8t4b3Rz
    ► Become a Patreon for exclusive tutorials👉 www.patreon.com/anthonygg_
    Thanks for watching

  • @kiboit
    @kiboit Год назад +43

    That subtle GF joke totally sent me 🤣🤣You said it without flinching too. Thank you for this , its actually really helpful

  • @fersalamanca2606
    @fersalamanca2606 Год назад +3

    thanks, I have struggled a bit to understand the whole concept of goroutines, channels and wait groups, but this cleared all doubts.

  • @augustocaro43
    @augustocaro43 Год назад +1

    This is the best example on Golang concurrency I could ever come across. Period.

  • @jameslapointe3044
    @jameslapointe3044 2 года назад +2

    Great video that helped my a lot make sense. At times - you seem to be running too hot trying to explain/do too much but at end I understood everything.

  • @alexsandersilva7402
    @alexsandersilva7402 8 месяцев назад

    Great. I'm learning Go and using channels, go routines and waitgroups to read and check information from multiple files.
    I'm using select and a quit channel to control the end of processing.

  • @hmls3579
    @hmls3579 Год назад +5

    what could be a good match for Bob? Alice! goddamn Alice!!!

  • @terjemah_alquran
    @terjemah_alquran Год назад +1

    i really enjoy how you tell something with some jokes, thank you 🤣

  • @redditallstar7572
    @redditallstar7572 2 месяца назад

    This serves as a good example of aggregating data but a lot of the time you are connecting to an external data source which can throw errors on your method calls, hope in the future if you re visit this you show

  • @kke
    @kke 2 года назад +43

    Or you could just do `resp := make(any, 2); for i := range resp { resp[i] =

    • @anthonygg_
      @anthonygg_  2 года назад +10

      True! Thanks for this.

    • @KACP1999
      @KACP1999 2 года назад

      @kke can you explain how this works:) ?

    • @kke
      @kke 2 года назад +11

    • @johnxisde
      @johnxisde Год назад

      @kke thank you for this tip!

    • @gmeister3022
      @gmeister3022 10 месяцев назад +4

      In this case, you're right. But it's only viable if the number is static (in this example, the size of the buffer is 2). We would've had a different case if it was an unbuffered channel.

  • @swapnilkant2793
    @swapnilkant2793 3 месяца назад

    Amazing Explanation!!

  • @faisalmushtaq2287
    @faisalmushtaq2287 Год назад +1

    Awesome video, Thanks. Must say that all other videos on goroutines on YT are useless except for yours.

  • @seanknowles9985
    @seanknowles9985 2 года назад +2

    Quality videos for the fans!

  • @sanjarmatin6227
    @sanjarmatin6227 9 месяцев назад +1

    Good job man 😊

  • @PramodSetlur
    @PramodSetlur 10 месяцев назад +2

    Great videos, thank you for them!
    I have a question: How come the channel itself is being passed around to various functions, and not the pointer to the channel? Wouldn't we need a pointer to the channel, so that the data is persistent once all the go routines complete? I guess I am missing something.

    • @anthonygg_
      @anthonygg_  10 месяцев назад +1

      Channels are syntactic sugar. Thats all being handled by the compiler.

  • @hassanad94
    @hassanad94 8 месяцев назад

    Thanks finally i understand it :)

  • @wmatheushenriq
    @wmatheushenriq Год назад

    Excellent explanation!! Thank you

  • @Ladyhoat
    @Ladyhoat 2 года назад +1

    Great explanation. Thanks!

  • @sovrinfo
    @sovrinfo 2 года назад +1

    Awesome video. Thanks!

  • @vp--
    @vp-- Год назад +2

    Hi , great explanation . Question : in this example when ranging over respch how do you know 1st item will be like or the match ? Is it random or in order of function calls ?

    • @embrace7972
      @embrace7972 Год назад +2

      same question

    • @marinm3869
      @marinm3869 Год назад

      Usually it depends, whichever function writes to the channel first. If you fetch from a remote resource you don't know what value will be written to the channel first because you don't know the latency, but in this case you know the name will come first since the function sleeps 100 ms and then writes to the channel while the likes function sleeps for 150 and then writes. The order of the function call does not matter since they are concurrent. Hope i was clear :)

    • @embrace7972
      @embrace7972 Год назад +2

      @@marinm3869 Yes you are right. But what would happen in the case of an API calls? I would never know how much time it would take. Then how will I know which API call am I reading from the channel. How are we supposed to differentiate the values from the channel?

  • @naveedurrehman9914
    @naveedurrehman9914 Год назад

    its very informative video indeed. thank you sir

  • @alst4817
    @alst4817 6 месяцев назад

    Love your accent man, where u come from? Sounds a wee bit Dutch but also not😂

  • @monawwarabdullah7438
    @monawwarabdullah7438 Год назад +1

    thanks for sharing !!

  • @codertjay
    @codertjay Год назад

    love this channel

  • @Jackbeti
    @Jackbeti Год назад +1

    Perfectly ❤

  • @benkogan1579
    @benkogan1579 10 месяцев назад

    Wait group is one way to solve this. Another is to just iterate over the number of tasks (2) and receive from the channel. No need to close, it will get GC'ed as usually.

  • @DillPL
    @DillPL 2 года назад +4

    If we know for how many routines we have to wait, why not use regular indexed for loop?
    what is the benefit of using the wait group?
    for i:= 0; i

    • @anthonygg_
      @anthonygg_  2 года назад +8

      The waitgroup is used so we know when to close the channel. When I think about it, your approach could also work. But I thought showcasing "ranging over channels" and "sync.Waitgroup" was important.

  • @arunupadhyay3002
    @arunupadhyay3002 7 месяцев назад

    Question:- what if my go routine is sending data and i want to manipulate the same in main function . This seems to be sync behavior as we are collecting response in channel and then closing it and later we are manipulating the same. Can you suggest some approach how it can be done in my scenario as it keeps throwing error. Thanks in Advance.

  • @ehSamurai3483
    @ehSamurai3483 9 месяцев назад +1

    If we are querying from DB, I don't think goroutine would be much helpful since its an I/O operation.

  • @dmitriyobidin6049
    @dmitriyobidin6049 11 месяцев назад

    What if we need to zip responses from 2 different channels.

  • @annguyen6166
    @annguyen6166 Год назад

    Hi Anthony, very love your golang video. But i still don't known when we should use gorountine and handle gorountine correctly, can u help me

  • @emzx111
    @emzx111 Год назад

    is it good practice to emit to same channel despite multiple different api calls?

    • @feryadialoi2244
      @feryadialoi2244 Год назад

      it is just a demo, you probably don't want to mix the response type with a generic one like any, or you actually can mix it by embracing the usage of a generic type

  • @xelesarc1680
    @xelesarc1680 Год назад

    Thats a good example maybe with real database con reall and some call api too

  • @hectoralvarado9778
    @hectoralvarado9778 Год назад +1

    Understood everything except the part of GF what is a GF it's a Type or library??

    • @anthonygg_
      @anthonygg_  Год назад

      Girlfriend

    • @twitchizle
      @twitchizle 11 месяцев назад +1

      ​@@anthonygg_you missed the joke damn

  • @gusary8439
    @gusary8439 Год назад +1

    i still dont understand how to handle if function userLikes returns string and error (not only 1 return)

    • @davidblake8612
      @davidblake8612 11 месяцев назад

      I dunno either, but maybe a user defined struct of response and error? Make the chan of that type?

  • @rogerramjet69
    @rogerramjet69 2 года назад +1

    how do you specify a key from the channel... say in the loop I just want to print likes...something like if respch.key == 'likes' print blah

    • @anthonygg_
      @anthonygg_  2 года назад +1

      for key := range channel. So your that will be the key you are looking for.

    • @rogerramjet69
      @rogerramjet69 2 года назад

      @@anthonygg_ hey mate that wont work... we havent assigned a key:value pair to each of those go routines. tried using make(chan map[string]any, 2) but seems very messy :/

    • @rogerramjet69
      @rogerramjet69 2 года назад +1

      @@anthonygg_ i created a struct respch := make(chan UserStuff, 2) and then in the funcs i used UserStuff{key: "likes", value: 11} and UserStuff{key: "match", value: "ANNA"} which appears to work ok... not sure if there is a cleaner approach.

    • @anthonygg_
      @anthonygg_  2 года назад

      @@rogerramjet69 You should be good. If you want to discus this more in-depth you maybe join my Discord community. That chats a bit better.

    • @seanturner7400
      @seanturner7400 2 года назад +1

      @@rogerramjet69 I think that's a way cleaner approach to be honest, I thought any was really suss. I would create two structs however, `userLikes{likes: int, error: err}` and `userMatch{match: str, error:err}`. This pattern is more intentional and also lets you check for errors.

  • @terranova1995
    @terranova1995 6 месяцев назад

    Typing that fast!

  • @ryanleemartin7758
    @ryanleemartin7758 Год назад +5

    "it waiting but it never comes... just like your gf". ☠

  • @alexmeh1919
    @alexmeh1919 Год назад

    🤙🤙🤙

  • @manojsinghnegi1565
    @manojsinghnegi1565 Год назад +1

    🤙🤙🤙🙏🏽🧐

  • @tobiahrex
    @tobiahrex 5 месяцев назад +1

    Uhhh you only called two functions with the go routine's and the runtime was the SUM of both of them. You didn't solve the problem you wanted; combing their runtime so the max total was the worst single.

    • @SpiderWebGhost
      @SpiderWebGhost 4 месяца назад

      actually the 250 ms is the sum of first non goroutine function fetchUser() which takes a 100 ms and then the worse of the two functions that are goroutines, so max(150,100) which in total gives us 250. Initially without goroutines, the sum was 350 ms

    • @mohammadahzan3764
      @mohammadahzan3764 3 месяца назад

      Check the third fucn which takes 100s i believe that solves your problem

  • @rahulsriram6295
    @rahulsriram6295 2 года назад +3

    so this is equivalent to Promise.all in JS?

    • @mimamch
      @mimamch Год назад

      I have same opinion