Pros vs Cons of When to USE Pointers in Golang

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

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

  • @MelkeyDev
    @MelkeyDev  7 месяцев назад +11

    I hope you all enjoy this video! Let me know what you think in the comments

  • @themichaelw
    @themichaelw 7 месяцев назад +24

    *context* I've been working full time in Go at an org with > 1000 engineers for the past 2 years, and I've read The Go Programming Language cover to cover. So I'm a decent gopher by most standards.
    I pretty much only _pass_ pointers when mutability of the original object is desirable. I _use_ pointers in structs when a value is optional, eg. when parsing from json. Objects like slices and maps are *no more efficient to pass to as a pointer than by value*, since the "value" of a slice/map is a "header" struct with a 3 fields denoting length, capacity, and a pointer to the first address of the slice. So there's already a pointer under the hood, no need to create another. Also, pass-by-value is typically faster for small things. And if you're running on 64 bit hardware and you're passing pointers to < 64 bit variables like int32, you're literally using twice as much space to pass that as a pointer than you would to just copy the 32 bit value, since a pointer will be full word, or 64 bits on that architecture.

  • @fersalamanca2606
    @fersalamanca2606 7 месяцев назад +13

    I think that for us Go noobs some code examples illustrating these points would be hugely benefitial

  • @rosehogenson1398
    @rosehogenson1398 7 месяцев назад +13

    Whenever you return an invalid value from a function, either nil or an uninitialized struct, it's good to include an extra bool (or error) return as well. For example:
    func f() (*MyStruct, bool) {
    if !somePrecondition() {
    return nil, false
    }
    return &MyStruct{}, true
    }
    This greatly reduces the chance that the caller will forget to check for nil, and get a nil pointer dereference. This comes from the builtin map type, and the _, ok := pattern.

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +2

      OOOO
      This is awesome.
      And if you want to return an error, would you just append it?

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

      thanks, makes sense

  • @cristophermoreno2290
    @cristophermoreno2290 7 месяцев назад +8

    🔥I use pointers to:
    👉 `mutate` data between 2 isolated `code blocks (functions)`
    👉 Avoid new memory allocations
    👉 make the Garbage Collector happy ...
    Thank you for the great video !

  • @afsdab
    @afsdab 7 месяцев назад +5

    I'm learning Go and for now I'm returning in every function I have, but I watched a bunch of videos that recommend if I'm getting info and I won't do any change in the state the function should not return a pointer.
    I'll give a try for this approach and see what happens

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +3

      I think thats a good rule of thumb.
      In all honestly, on smaller scale apps, either approach will be good

  • @justintie
    @justintie 7 месяцев назад +13

    Copying a struct is much faster than messing with pointers, unless it is a huge struct

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

      Yep. If its a huge struct then copying becomes inefficient

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

      This is why I think mixed API isn’t a strong con. It may be warranted for performance, and doesn’t strike me as very different from simply returning different types.

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

      Yeah, gotta consider the overhead of the GC having to perform escape analysis on pointers. Most cases copying is faster simply because the value is just on the stack and is easily cleaned up when the function returns.

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

    2 More pros:
    - Can use methods with both pointer and value receivers.
    - Does not copy the sync.Mutex

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +1

      Aye awesome thank you

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

    Great vide Melkey, I understand more about pointers in Go awesome language!!

  • @jordangregory9852
    @jordangregory9852 7 месяцев назад +2

    Great video! Personally I’m purely in the “pointer for mutability only” camp. I default to copies so I can’t mutate things on accident.

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +1

      Hell yeah. Love it

  • @Zmey5656
    @Zmey5656 6 месяцев назад +1

    I used to think that the pointer in GoLang was an advantage over other languages, but now, after your video, I understand that we have different opinions about the pointer.

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

      thats good yeah??

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

      @@MelkeyDev Yep))))

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

    What do you think about filling up DB with values instead of nil? I've been bitten by this before where a nil db field could not be scanned into a struct field. It was a runtime error.

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

    I started learning GO, a few months ago. How we can structure our codes to prevent import cycle errors

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

    Hey @MelkeyDev do you have any suggestion for a book/course/any other resource to learn computer science concepts in Go?

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +1

      ruclips.net/video/qT14b1pxtiI/видео.html&t I made this video - let me know if its helpfu

  • @erikslorenz
    @erikslorenz 7 месяцев назад +1

    I tend not to use pointers when it’s record type values. For example user. A lot of the times it’s coming out of a context and I need that to be a real val

  • @emil_l889
    @emil_l889 7 месяцев назад +1

    Once spent like an hour getting nil pointer deference when working w redis only to realize that I forgot to initialize the struct😭😔

  • @Phoony17
    @Phoony17 Месяц назад

    I think you are forced to use pointers when variables from a struct are optional, ex: an optional number field in a document of mongodb, if the number is zero you can't tell the difference if it's in the DB or if it was zero'd by the struct. But if you use a pointer you'll have nil if it's not in the db.

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

    How are mutability and nil as value good things?

  • @AntonLukas
    @AntonLukas 7 месяцев назад +8

    Most of the downsides appear to just be skill issues

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +4

      Everything is a skill issue if you break it down

    • @AntonLukas
      @AntonLukas 7 месяцев назад +1

      @@MelkeyDev Fair enough, great video by the way.

  • @paca3107
    @paca3107 7 месяцев назад +1

    Please make a video how to avoid memory leaks!

  • @ofadiman
    @ofadiman 7 месяцев назад +2

    Could you point me to the nearest grocery store using golang?

  • @kenzo3477
    @kenzo3477 7 месяцев назад +3

    use pointers in C 🚫
    use pointers in go ✅

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

    Great video

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

    I only use pointers when i purposelly want to modify the struct somewhere.

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

    Using pointers should be an intentional and deliberate action. If you are not sure, don’t use it until you are.
    I’ve seen plenty projects where EVERY struct is a pointer and it sucks big time.

  • @luizpbraga
    @luizpbraga 7 месяцев назад +1

    return a stack pointer is just weird

    • @MelkeyDev
      @MelkeyDev  7 месяцев назад +1

      its all weird

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

    Nah always give the shit struct back and an error