Golang Context Explained - How To Use With Timeout

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ม.ค. 2025

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

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

    I hope you all loved this video! Leave a like, comment, like + subscribe. It means the world to me. Let me know what video you want to see in the future!

  • @Urgleflogue
    @Urgleflogue 9 หลายเดือนก่อน +11

    You should put the long call in a goroutine with a loop and switch. What you've done here is not what contexts are for, you could've just measure the elapsed time and return error or otherwise.

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

      Could you provide an example? This is what I'm trying to do right now and struggling

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

      @@ashleyspianoprogress1341 th-cam.com/video/kaZOXRqFPCw/w-d-xo.html

  • @JohannesLuebke
    @JohannesLuebke ปีที่แล้ว +38

    But are we still waiting for 400ms? We are just not returning the result but an error right? Or how do we reach the ctx.Err() check before the 400ms run out?

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

      Great question. I thunk it should be pretty easy to reproduce and test

    • @tomas36181
      @tomas36181 4 หลายเดือนก่อน +2

      @@fyrofux You're supposed to wait for the result in a separate goroutine and there's a way to check whether the context finished earlier than the func call. That way you're not waiting for no reason. This example is just bad.

  • @0xZeeS
    @0xZeeS ปีที่แล้ว +36

    Great video!
    However, example could be better - since the function still blocks until the time it takes for the API call. I.e. if API call blocked for an hour, you'd need to wait for the whole hour, only to encounter deadline exceeded error.
    ^ Nevertheless, this vid presents a good starting point!

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

      where would you error using the context in that case?

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

      @@kartashuvit4971 have a for loop checking for the error then kill the http request

  • @ronny584
    @ronny584 11 หลายเดือนก่อน +10

    A video that is 8 minutes long with a trash thumbnail, ends up with a trash example. This channel is cooked lmao.

  • @devfortunato
    @devfortunato 8 หลายเดือนก่อน +4

    You should have used a goroutine with a select statement and check the ctx.Done() or the result. What are you doing in the example is just waiting for the result and throwing away if the time is exceed.

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

    If the func in sleeping for 400ms (didn't reach the if statement) and the context is cancelled, how the "context timeout exceeded" error is reached?

    • @hakooplayplay3212
      @hakooplayplay3212 ปีที่แล้ว +10

      it actually not canceled and continue to work. its just throw result out, because its too late. So this example is trash :) you actually need to cancel this function call in mid air if time is exceeded, so you need to run it in gorutine and wait outside in select for result or ctx.Done(), to not wait for this call to return something in a minute for example to just throw it away.

  • @BanAaron
    @BanAaron ปีที่แล้ว +9

    Could you boost the audio levels on your videos? I have to have my laptop speakers at 100% to be able to hear clearly, it is only with your videos

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

      Laptop speakers? Gotta get that upgraded player!

  • @GoWithAndy-cp8tz
    @GoWithAndy-cp8tz หลายเดือนก่อน

    Hi! What model of headphones do you use in this video? I'm searching for comfortable and good-sounding headphones and yours look quite promising. Great video by the way. Cheers!

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

    Context is really cool actually, good stuff

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

    your explanation is awesome bro

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

      Thank you so much 😀

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

    Nice video. Simple to the point

  • @AnilGupta-v1c
    @AnilGupta-v1c 2 หลายเดือนก่อน +1

    It's wrong example the sleep call is blocking call so it will still wait and just return deadline error after waiting for sleep time so the whole purpose of using context is defeated @MelkeyDev should correct the video or atleast delete the video so that your viewers are not mis-guided

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

      I have another video where I explain Go Context and makes the correction

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

    Great video! Any chance you can provide something on Authentication/Authorization with Go? Thanks!

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

    Only context go needs is enums, sadge

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

    Thanks for the great content. It would be nice to have less jumping around between zoomed in text editor / your camera / split view. Just sticking to split view would be easier to watch / follow. Thanks again.

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

      Thanks for the feedback - Will do this for my next tutorial :)

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

    MELKEY MELKEY MELKEY

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

    how far are you in Berserk?

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

      All caught up again

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

    How do I get the values of an associative array in PHP?

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

      i think it's in the go docs somewhere

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

      arr[“key” => “value”]
      $value = arr[“key”]

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

    Great examples as always, Melkey

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

    fix the audio pls

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

    Used my actual userID😮

  • @AbrahamHalfi
    @AbrahamHalfi 9 วันที่ผ่านมา

    dude I'm so jealous you can just code away at whim like that
    major fomo and major jealousy

  • @jeffreysmith9837
    @jeffreysmith9837 7 หลายเดือนก่อน +1

    this is not it. your time.Sleep() is blocking