Go Error Handling Best Practices & Advanced Tips

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

КОМЕНТАРІ • 49

  • @GolangDojo
    @GolangDojo  Рік тому +2

    Get your *FREE Golang Cheat Sheet* -
    golangdojo.com/cheatsheet

  • @Emonadeo
    @Emonadeo Рік тому +70

    Great video, bad clickbait thumbnail

  • @RichM1967
    @RichM1967 Рік тому +20

    Error handling in Go is not a mistake -- it's very good and I don't like working with languages that doesn't have something like Go's errors. What I want is a better way to handle those errors. What I would like is a function similar to 'return'.
    file, err:=os.Open("my file.txt")
    err.return

    • @_slier
      @_slier Рік тому +3

      its totally a mistake.. it outrageous.. but the golang author don't want to admit their mistake.. golang codebase littered with error handling. still not a mistake?

    • @tshwarelolebeko4223
      @tshwarelolebeko4223 Рік тому +2

      ​@@_slier what were you hoping for, try-catch?, nah that's true unnecessary with such a powerful and readable error handling system, although I love rust's error handling more, Go is also as best compared to try-catch

    • @mrredogaming8885
      @mrredogaming8885 Рік тому +1

      @@_slier if you wanted to handle every error in other languages you would have to create multiple try catch statements, but in golang you are required to handle every single error, for example making an API would provide better API errors

    • @kyoukisu3628
      @kyoukisu3628 11 місяців тому

      ​@@mrredogaming8885rust be like: okey, fine

    • @zanez7953
      @zanez7953 11 місяців тому +2

      ​@@_slier Codebases handling errors is a good thing

  • @nanonkay5669
    @nanonkay5669 Рік тому +8

    I don't understand the repetitive part that people don't like. Isn't error handling occur only as often as the number of functions that can potentially return an error? If in TS you're utilizing 20 async fxns that can error, that's 20 try-catches you need to write. Wouldn't that be called repetitive too? It really doesn't matter what language you're using, errors are going to want to be handled as often as the number of functions used that can cause them. And Go syntax to deal with them is a very short if statement

    • @ViniDerp
      @ViniDerp Рік тому +2

      In TS you wouldn't necessarily have to treat errors in all of those 20 async fn calls, you can just put a try/catch block on top, which is what we often see.

    • @nanonkay5669
      @nanonkay5669 Рік тому +4

      @@ViniDerp but if you have 20 distinct fxns that can potentially error, you need try catch all of them, if you want to get appropriate errors for each function. Putting them all in one try catch won't be helpful because if one errors, you dunno which one

    • @ViniDerp
      @ViniDerp Рік тому

      @@nanonkay5669 I agree with you, in complex scenarios it's the best practice to do that, but let's be honest, most of the code that is in production out there doesn't have that, same thing for java.

    • @KingStructre
      @KingStructre 11 місяців тому +1

      @@ViniDerp And yet when issue in production happens because all those try and catch, you can't tell what cause it because most of the time each developer handle it different, golang error handling enforces the best practice, handle it in small slice, and not when it part of bigger slice. I hope golang won't change error handling, because all other langauges do it wrong (talking as Java developer of 10 years which moved to golang), I am talking about projects with lot of different developers overtime, if it's a proejct when you write on your own and you know your code best, than I don't mind the try/catch or golang error handling way, but you need to remember that code is team effort (most of the time).

    • @ViniDerp
      @ViniDerp 11 місяців тому

      @@KingStructre Indeed, in large teams, breaking error handling into small pieces is a good way to ensure readability and maintainability. The only problem is lacking freedom to not do so, not all projects will fit this scenario, it can be quite verbose and delay development process, which is one of the reasons I define golang a niche language, not all companies will benefit from it.

  • @CommonWebDev
    @CommonWebDev Рік тому +4

    I love your videos. I am newbie in Golang, and i really appreciate your explanation and clear examples 👀

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

    Personally, since I'm using error handling everywhere on my microservices build in Go, Our team has built a centralized library where we can use those Error handling, this way we can remove code duplications in other services.

  • @RoyRope
    @RoyRope 10 місяців тому +2

    The Go try proposal has been declined.

  • @k98killer
    @k98killer Рік тому +2

    Would it not be faster to use a custom error struct that does not live on the heap? RAM access makes a much bigger performance hit than returning something that lives only in the callstack, right?

    • @disculpa
      @disculpa 9 місяців тому

      I don't know anything about go but if the error can be nil I doubt it's stored on the stack.

    • @Rakstawr
      @Rakstawr 5 місяців тому +1

      Not exactly how go works in allocating memory. Just because a var is an interface and can be nil pointer doesn't mean it is on the heap automatically.

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

    Why can´t I just do:
    func customError(err error, msg string) {
    if err != nil {
    fmt.Println(err, msg)
    }
    }
    Or put anything you want inside the if, ErroF, log. etc?

  • @antoniothomacelli
    @antoniothomacelli Рік тому +1

    I pay something $600 in course of Wallace (Golang Dojo) and UA-cam Videos free it is more complete than course... (SAD)

  • @_slier
    @_slier Рік тому +3

    2:14 look at the date of proposal.. that sum up golang core developer attitude.. no shit..

  • @felipeazevedo2279
    @felipeazevedo2279 Рік тому

    Great content!

  • @FedericoPedemonte
    @FedericoPedemonte Рік тому +1

    caught a TooManySError at 3:05 😅

  • @staystealth
    @staystealth 9 місяців тому +2

    nice clickbait

  • @BenjaminLupton
    @BenjaminLupton Рік тому

    Discovered your youtube channel today and very much appreciate it! Your english is also very good, with the only shortcoming is that sometimes your pronunciation of "r" includes a "w" sound; perhaps you can hire a tutor to help with that as it can be distracting and harder to understand. Considering everything else is perfect, this would be a small fix that could go a long way. Keep up the great work, and I look forward to watching more of your videos and following your progression! Well done.

  • @disculpa
    @disculpa 9 місяців тому

    This seems idiotic to me. The code is gonna be cluttered with ugly error checks after every function call. 99% of the time you're just gonna want to return immediately on error. Newbies catching and handling errors unnecessarily is the cause of so many confusing bugs and this is just going encourage people to do that more.

  • @artemsedykh
    @artemsedykh 9 місяців тому

    I feel that the content in this video is outdated for several years and the author does not know about join, is, as etc.

    • @nyahhbinghi
      @nyahhbinghi 8 місяців тому

      join what?

    • @artemsedykh
      @artemsedykh 8 місяців тому +1

      @@nyahhbinghi About the errors.Join/errors.Is/errors.As.

  • @ray-lee
    @ray-lee Рік тому

    其實你係咪含住粒野黎講野?

  • @MichaelKondrashin
    @MichaelKondrashin 8 місяців тому

    What is presented here is WRONG. Global capitalized variable "Err... = errors.New("...") should be used EVERY TIME you return custom error and not when they are common! This gives to user of your code ability to distinguish between various errors you return:
    err := someFunc()
    if errors.Is(err, Err...) {
    Some action
    } else {
    ...
    }
    Additionally, these errors should be wrapped to add context:
    var ErrNegative= errors.New("negative parameter")
    func foo(x int) error {
    if x < 0 {
    return fmt.Errorf("x = %d: %w", x, ErrNegative)
    }
    ...
    }
    This gives ability to show error message "as is" or check whenever this is some particular error.
    For more advanced cases, you can create hierarchy of errors, i.e.:
    var (
    ErrParameter = errors.New("wrong parameter")
    ErrNegative = fmt.Errorf("%w: negative value")
    )

  • @bigtymer4862
    @bigtymer4862 Рік тому +1

    Try 🤮🤮🤮

  • @flatearthtravolta6585
    @flatearthtravolta6585 Рік тому +1

    This is terrible.

  • @user-lz1gs1yl9r
    @user-lz1gs1yl9r Рік тому

    I really appreciate your work but there is no useful or new information at all in this video

  • @StEvUgnIn
    @StEvUgnIn Рік тому

    lol

  • @codewithtee
    @codewithtee 10 місяців тому

    I created a gofaker libary, do you mind helping to review it on your channel?

  • @tmahad5447
    @tmahad5447 Рік тому

    BEST Channel to learn Go. Dedicated channel on golang. Golang dojo

    • @jed271
      @jed271 Рік тому +2

      Nah, anthonygg better

    • @tmahad5447
      @tmahad5447 Рік тому

      @@jed271 So why you're here!

    • @mSpinks01
      @mSpinks01 Рік тому

      @@jed271 facts, but GoLang dojo is good as well