Defer Functions In Golang: Everything You Need To Know

Поділитися
Вставка
  • Опубліковано 6 лис 2024

КОМЕНТАРІ • 14

  • @dawnrazor
    @dawnrazor 2 місяці тому +2

    Good video, but there is an essential detail about the defer statement that you omitted which can be a really awkward gotchya if you’re not aware of it. And that is a defer creates a closure over local state. Your defer statement must make sure that any state that it interacts with is fully present at the time of the defer statement. Eg, let’s say the first thing inside of a func is a var declaration of an uninitiatialised variable. Next there is a defer that interacts somehow with this state. After the defer there is some logic that updates this state. If you think that when the defer actually runs it will see the updated version of that state, you will be surprised to learn that the version of that state the defer sees is the uninitialised version. This was a shock to me when I first discovered it and took me quite a while to realise what was happening. This occurs because the defer is working on the closure version of the state and hence only sees the uninitialised version. Hope this helps anyone to not fall into this subtle trap.

    • @rumenneshev9433
      @rumenneshev9433 2 місяці тому +2

      This not always true, it depends on the defer declaration
      Test it with:
      func work() {
      var v string
      defer func() {
      fmt.Println("V in func's defer", v)
      }()
      v = "asd"
      fmt.Println("V in func", v)
      }
      Outputs:
      V in func: asd
      V in func's defer: asd
      BUT:
      func work() {
      var v string
      defer fmt.Println("V in func's defer", v)
      v = "asd"
      fmt.Println("V in func", v)
      }
      Will indeed output:
      V in func: asd
      V in func's defer:

    • @dawnrazor
      @dawnrazor 2 місяці тому

      @@rumenneshev9433 ok I’m not going to dispute your case, but my point still stands. Beware of how you do defer

    • @rumenneshev9433
      @rumenneshev9433 2 місяці тому

      @@dawnrazor Yes, not meant to argue, just to note that there's a way to work around this defer "problem". Still - Beware of how you do defer

  • @Majiy00
    @Majiy00 3 місяці тому

    Great videos, you deserve more views, subscribed!

    • @FloWoelki
      @FloWoelki  3 місяці тому +1

      Wow, thank you so much 🥺

  • @SoyaibZihad10
    @SoyaibZihad10 Місяць тому

    great video. can you name the font please?

    • @FloWoelki
      @FloWoelki  Місяць тому +1

      Thank you! I am using the Monaspace font :)

    • @SoyaibZihad10
      @SoyaibZihad10 Місяць тому

      @@FloWoelki great. thanks for the reply.

  • @youknownothing_
    @youknownothing_ 3 місяці тому

    noticed you are using zed, which theme is that?

    • @FloWoelki
      @FloWoelki  3 місяці тому

      It's the GitHub theme :)

  • @bravian7958
    @bravian7958 4 місяці тому

    which ide do you use

    • @FloWoelki
      @FloWoelki  4 місяці тому

      I am using Zed at the moment :) I've also made a quick video about Zed, if you want to check it out.

  • @RaffayDoesTech
    @RaffayDoesTech 20 днів тому

    loved it